Sunday, July 09, 2006

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



3 comments:

rudram_1987 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

Gokull G said...

rudram_1987:u have to declare it as long int