C program to reverse a number and check weather palindrome

This program reverses an integer number. Palindromes are those numbers whose values is same if you read it from left to right or right left. So if a number and its reverse is same it is a palindrome.

Here we divide the number with 10 and extract the remainder. This remainder is always the digit in the unit place of the number. The quotient is again divided with 10, and this continue till the quotient falls below 0. The remainder extracted in each division by 10 is added to form the reverse number.

 

  1. //Program to reverse a number and check weather it is palindrome or not
  2.  
  3. #include<stdio.h>
  4. main()
  5. {
  6.   int rem,n,dup,rev=0;
  7.   printf("\nEnter an integer no : ");
  8.   scanf("%d",&n);
  9.   dup=n;
  10.   for(; n>0; n/=10)
  11.      {
  12.       rem=n%10;
  13.       rev=(rev*10)+rem;
  14.      }
  15.   printf("\nReverse of %d is %d",dup,rev);
  16.   if(rev==dup)
  17.      printf("\n %d is a palindrome",dup);
  18.   else
  19.      printf("\n %d is not a palindrome",dup);
  20. }
  21.      
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s