C program to find given number is Palindrome or not

The given number is palindrome or not

A number is said to be a palindrome if the reverse of the number is same as the original number, for example: 121,11,343,etc.,

INPUT: 878
OUTPUT: The given number is not a palindrome
In the above example as the reverse of the number is same as the original number the number is said to a palindrome number.

INPUT: 23  
OUTPUT: The given number is not a palindrome
In the above example the reverse of the number is not same as the original number the number is not a palindrome number.

Program

/*C program to find the given number is palindrome or not*/

#include<stdio.h>
#include<conio.h>    //header files
int main()
{
int n, r, rev = 0, temp;
printf("\n Enter a number: ");
scanf("%d ", &n);
temp = n;      //storing the value in an temporary variable
do
   {
     r = n % 10;
     rev = (rev * 10) + r;
     n = n / 10;
   }while(n != 0);
(rev == temp) ? printf("\nThe given number is palindrome") : printf("\nThe given number is not a palindrome");  // ternary operator
return 0;
}

Output:
Enter a number: 232
The given number is palindrome

Program explanation 

1. stdio and conio are header files which are used for standard input and output
2. #include is used to include the files
3. printf() is used to display the given text on the display screen
4. scanf() is used to take the given input
5. The given input is stored temporarily in temp variable
6. do while loop is an exit contorl loop
7. The reverse number is stored in rev variable
8. A ternary operator is used here [ condition ? true_statement : false_statement ] if condition is true then true statement will be executed else false statement will execute
9. If the rev is equal to temp then it will execute the true statement, if the rev is not equal to temp then the false statement will be executed   
10. return() is used to return the value to the main() 

No comments:

Post a Comment