w3resource

C Exercises: Display the number in reverse order

C For Loop: Exercise-37 with Solution

Write a program in C to display the number in reverse order.

Pictorial Presentation:

 Display the number in reverse order

Sample Solution:

C Code:

#include <stdio.h> // Include the standard input/output header file.

int main(){
    int num, r, sum = 0, t; // Declare variables for the original number, remainder, reversed number, and temporary variable.

    printf("Input a number: "); // Prompt the user to input a number.
    scanf("%d", &num); // Read the input from the user.

    for(t = num; num != 0; num = num / 10){ // Loop to reverse the digits of the number.
         r = num % 10; // Extract the last digit (remainder when divided by 10).
         sum = sum * 10 + r; // Build the reversed number by adding the extracted digit.
    }

    printf("The number in reverse order is : %d \n", sum); // Print the reversed number.
	 return 0;  // Indicate that the program has executed successfully.
}

Sample Output:

Input a number: 12345                                                                                         
The number in reverse order is : 54321 

Flowchart:

Flowchart : Display the number in reverse order.

C Programming Code Editor:

Previous: Write a program in C to display the such a pattern for n number of rows using a number which will start with the number 1 and the first and a last number of each row will be 1.
Next: Write a program in C to check whether a number is a palindrome or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.