C Program To Reverse a Number using Loops
Learn How To Reverse A Number in C Programming Language. It is important that we should know How A For Loop Works before getting further with this C Program Code.
To Reverse the Digits of an Integer, we need to Extract every End Digit using Modulus Operator and then store it in a Sum variable. Then, keep on adding the other digits to the Sum variable.
Example
Reverse of 1234 = 4321
Method 1: Reverse an Integer with While Loop in C Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> int main() { int num, sum = 0, rem, temp; printf("\nEnter a Number:\t"); scanf("%d", &num); temp = num; while(num > 0) { rem = num%10; sum = (sum*10) + rem; num = num/10; } printf("\nReverse of %d:\t%d\n", temp, sum); return 0; } |
Method 2: Reverse a Number using For Loop in C Programming
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> int main() { int num, sum = 0, rem, temp; printf("\nEnter a Number:\t"); scanf("%d", &num); for(temp = num; num > 0;) { rem = num%10; sum = (sum*10) + rem; num = num/10; } printf("\nReverse of %d:\t%d\n", temp, sum); return 0; } |
Method 3: C Program Code To Reverse a Number using Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> int reverse(int x); int main() { int num, res; printf("\nEnter a Number:\t"); scanf("%d", &num); res = reverse(num); printf("\nReverse of %d:\t%d\n", num, res); return 0; } int reverse(int x) { int rem, sum = 0; while(x > 0) { rem = x%10; sum = (sum*10) + rem; x = x/10; } return sum; } |
Method 4: C Code To Reverse an Integer using Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<stdio.h> #include<math.h> int reverse(int var, int limit) { if (limit == 1) { return var; } else { return (((var%10)*pow(10, limit - 1)) + reverse(var/10, --limit)); } } int main() { int num, temp, length = 0, result; printf("\nEnter a Number:\t"); scanf("%d", &num); temp = num; for(; num > 0; num = num/10) { length++; } result = reverse(temp, length); printf("\nReverse of %d:\t%d\n", temp, result); return 0; } |
Output

In case you get any Compilation Errors with this C Program To Reverse the Digits of a Number or you have any doubt about it, mention it in the Comment Section.
how can I display reverse of 100,200,2000…etc as 001,002,0002…/etc
is there any code for it
I think the same method will work fine to display reverse of 100, 200 and such numbers.
Thanks for so many methods. This has helped me to.understand the difference between while loop and for loop so efficiently. Reverse Number C Program is also used as a parr of Palindrome Number if I am not wrong.
Is there any library function to reverse an integer in C Programming? It would be then so easy.
For Reversing a String, there is a function strrev(). I am not sure about Reversing Function for Integers. However, you can check the above function. Copy it into a new header file and include it in your program.
This is fucking amazing man! So many methods just to reverse an integer?? Thanks!!! 🙂
For (; num>0; num = num/10)
This is something that I found. I guess it meams that we can skip a part of the For loop without getting amh compilation error.
Thank you so much for the explanation of how to reverse a number in c programming.
In your second method to reverse an integer, the for loop can be modified to represent a normal for loop in the following way:
for(temp = num; num > 0; num = num/10;)
{
rem = num%10;
sum = (sum*10) + rem;
}
If I give 123 they answer will be 321 & 432 will be show means what codes be use ?
How to return a reversed number from helper function to main function without using array.
Can i get pseudocpde for any of above codes??
//this is how you reverse it without loops
#include
#include
int main(){
int num;
printf(“enter a 4 digits number\n”);
scanf(“%d”,&num);
int a=num/1000;
int rest1=num%1000;
int b= rest1/100;
int rest2=rest1%100;
int c=rest2/10;
int rest3=rest1%10;
int d=rest3;
int mun=(d*1000)+(c*100)+(b*10)+a;
printf(“the reverse of your number is %d”,mun);
return 0;
}
oops!!! this is for a 4 digit loops!!
we can’t reverse “001” by any of these methods…………