Friday 27 November 2015

Program to check whether a number is Palindrome or not

//Program to check whether a number is Palindrome or not.
#include<stdio.h>
#include<conio.h>

void main()
{
      int  number,reverse=0, temp; /* 'reverse' is  used to store the reverse of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the reminder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&number);
      temp = number; //copies the value of 'number' in 'temp'

// logic for palindrome (the reverse of the number is equal to the number)

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
         rem=temp%10; // calculating the reminder of the number 
     reverse=reverse*10+rem; // calculating the reverse of the number
     temp=temp/10; // dividing the number by 10
      } // end of while loop

       
 if(reverse==number) /* Checking if number entered by user and it's reverse number is equal. */
     printf("Number is Palindrome");
     else
     printf("Number is not Palindrome");

      getch(); // for holding the console screen
}

No comments:

Post a Comment