In this tutorial you will learn about the C program to Reverse a Number and its application with practical example.
C Programs to Reverse any Number
In this tutorial, we will learn to create a C program that will reverse any Number using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
- Conditional Statements in C programming.
- Arithmetic operations in C Programming.
Program to reverse any number:-
In c programming, it is possible to take integer input from the user and reverse any Number with the help of a C program. The reversing of a number means the digits of a number will be swapped from the first position to the last position. The C language has many types of header libraries that have supported functions in them, with the help of these files the programming is easy.
With the help of this program, we can reverse any Number.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the required variables for the program. 2. Taking the input number from the user. 3. Passing that number to a while loop. 4. Reversing that input number. 5. Printing the reversed number to the user. 6. End the program. |
Program to reverse any number:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* C program to Reverse a Number */ #include <stdio.h> int main() { //declaring the required variables for the program int n, rev = 0, remainder; //Taking the input number from the user for reversing it printf("Enter an integer: "); //scanning the input number from the user scanf("%d", &n); //Reversing the number from the user while (n != 0) { remainder = n % 10; rev = rev * 10 + remainder; n /= 10; } //printing the number in reverse order printf("Reversed number = %d", rev); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- n = it will hold the integer value.
- remainder = it will hold the integer value.
- rev = it will hold the integer value.
Input message for the user for the integer value.
Program Logic Code.
Printing output reverses any Number.