Reverse the number and check for Equality

A five digit number is entered through the keyboard.
Write a program to obtain the reversed number and to determine
whether the original and the reversed numbers are equal or not.


#include<stdio.h>
main()
{

int last_digit, number, next_digit, rev_num;

printf ("Enter the five-digit number that has to be reversed and checked for equality:");

scanf("%d", &number);

last_digit = number - ((number / 10) * 10); /*units place*/

rev_num = last_digit; /* 5 */

next_digit = (number / 10) - ((number / 100) * 10); /*tenth's place*/

rev_num = (rev_num * 10) + next_digit; /*54*/

next_digit = (number / 100) - ((number / 1000) * 10); /*hundred's place*/

rev_num = (rev_num * 10) + next_digit; /*543*/

next_digit = (number / 1000) - ((number / 10000) * 10); /*thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*5432*/

next_digit = (number / 10000) - ((number / 100000) * 10); /*ten thousand's place*/

rev_num = (rev_num * 10) + next_digit; /*54321*/

printf ("The Reversed Number is: %d",rev_num);

if (rev_num==number)

{
printf("\nEntered number and reversed number are equal\n");
}
else

{
printf("\nEntered number and reversed number are not equal\n");
}
}


The file can be downloaded at:
Download File



Comments

Unknown said…
hi,when i use above program and tried to enter number 12345
the result is -11215......
actually it is being overflowed.......
even i mentioned unsigned integer.....any answer is appreciated...on my mail id rudra1803@gmail.com
marwan said…
hi ,why we will add here 100,1000,10000,100000
Unknown said…
rudram_1987:u have to declare it as long int
Sksks said…
I think your code didn't work in last printf function. you have to edit like this.

#include
int main()
{
int num,a,b,c,d,e,x;

printf("Enter a five digit number : ");
scanf("%d", &num);

//separating digits of the number
e = num % 10;
d = (num/10) % 10;
c = (num/100) % 10;
b = (num/1000) % 10;
a = (num/10000);

//reversing the number
x = e*10000 + d*1000 + c*100 + b*10 + a;
printf("\n%d", x);
if(num!=x)
{
printf("\nthe reverse of the number %d is not equal number.", x);
}
else
{
printf("\nthe reverse of the number %d is equal number.", x);
}
return 0;
}

Popular posts from this blog

C Program - Calculation of Area and Circumference of a Circle using Pointers

Matchstick Game using C Programming

Generation of Fibonacci Sequence using Recursion.