Monday, 9 July 2012

Program to check the number for being Palindrome in C


Code -



/* Program to check a number for being Palindrome */

/* A Palindrome number is a number that such that if we will reverse the number, it will not change */

//___ Including header files as known as preprocessor directives _____

#include<stdio.h>

#include<conio.h> //____ Mainly included for the getch() function ____

int main() //____ Function from where the program execution starts _____
{
int number = 0, reverse_number = 0, temp_number = 0, remainder = 0;

//____ Variable declaration and initialization _____

printf("\n\t __ Program to check a number for being Palindrome ___");

printf("\n\n   Enter the Number - ");

scanf("%d",&number);

temp_number = number;

while(temp_number != 0) //___ Reversing the digit using while loop ___
{
remainder = temp_number % 10;

//____ Extracting the digits one by one till temp_number becomes 0 ___

temp_number = temp_number / 10;

reverse_number = reverse_number * 10 + remainder;

//___ Constructing the number ____

}

printf("\n\n   Reverse number of %d is %d ", number, reverse_number);

if (reverse_number == number)

/* Checking whether the reversed number is equal to the original number */

{
printf("\n\n\n\t\t ___%d is a Palindrome number___", number);
}
else
{
printf("\n\n\n\t\t ___%d is not a Palindrome number___", number);
}

getch(); //___ Holding the screent to display output ____

return 0;

/* return 0 tells the compiler that program has executed successfully

 without any error____*/
}

Follow Us on Facebook - Assignment Hub

No comments:

Post a Comment