Home »
C programs »
C number manipulation programs
C program to Reverse an Integer Number/ Program for Reverse Number in C
This program will read an integer positive number and reverse that number. For Example input number is 1234 after reversing number will be 4321.
Logic to reverse Number program
The logic behind to implement this program - Input Number, and run a loop until number is greater than 0, using remainder (Modulus) operator extract last digits and then dividing by 10, last digits will be removed. This process will be executed until number is not zero.
Reverse Number program in C
/* program to reverse an integer number.*/
#include <stdio.h>
int main()
{
int n;
int dig, revNumber;
printf("Enter an integer number : ");
scanf("%d", &n);
/*Reversing Number*/
revNumber = 0;
while (n > 0) {
dig = n % 10; /*get digit*/
revNumber = (revNumber * 10) + dig;
n = n / 10;
}
printf("Reverse Number is : %d\n", revNumber);
return 0;
}
Output
Enter an integer number : 1234
Reverse Number is : 4321
Reverse number using user-define function
/* program to reverse an integer number.*/
#include <stdio.h>
/* function: reverseNum
to reverse an integer number.
*/
int reverseNum(int num)
{
int sum = 0, rem;
while (num > 0) {
rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
return sum;
}
int main()
{
int n;
printf("Enter an integer number : ");
scanf("%d", &n);
printf("Reverse Number is : %d\n", reverseNum(n));
return 0;
}
Output
Enter an integer number : 1234
Reverse Number is : 4321
C Number Manipulation Programs »