Let’s see how to write a simple C program to Reverse a Number. I will use the “while loop” to reverse the number in this program. This program is very useful for school/college students and those who have recently been learning C programming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int number, reverse = 0, remainder; printf("Reverse a number in C\n"); printf("Enter a number:"); scanf("%d", &number); while (number != 0) { remainder = number % 10; reverse = reverse * 10 + remainder; number = number / 10; } printf("Reverse of the number is : %d", reverse); return 0; } |
Sample output of the above C program:
1 2 3 4 5 6 |
Reverse a number in C Enter a number:654987 Reverse of the number is : 789456 -------------------------------- Process exited after 3.483 seconds with return value 0 Press any key to continue . . . |
I hope this program clears your doubts on how to Reverse a number using C language.