Q:

C Program to Check Whether a Number is Palindrome or Not

belongs to collection: C programs - Loops and Iterations

0

A palindrome number is a number which remains same when read from front or backwards. For example some palindrome numbers are 121, 212, 12321, 454,etc. To check whether a number is palindrome or not, first we reverse it and then compare the number obtained with the original, if both are same then number is palindrome otherwise not.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program to Check Whether a Number is Palindrome or Not - Source code

int main()
{
   int n, reverse = 0, temp;
 
   printf("Enter a number you want to check\n");
   scanf("%d",&n);
 
   temp = n;
 
   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
 
   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);
 
   return 0;
}

Program Output

Case 1:

Enter a number you want to check
5252
5252 is not a palindrome number.


Case 2:

Enter a number you want to check
69996
69996 is a palindrome number.

Program Explanation

1. Accept a number from the user which the user want to check and store it in the variable n.

2. Declare another variable reverse and initialize it to 0.

3. Copy the input number to the another variable temp.

4. Run the while loop until temp becomes zero. Inside the loop, multiply the variable reverse with 10. Obtain the remainder of the input number (temp%10) and add it the reverse variable and store it in the same reverse variable.

5. Obtain the quotient of the input number (integer division by 10) and considering this as input number repeat the steps as mentioned above until the obtained quotient becomes zero.

6. When it becomes zero, using if,else statement check whether the reversed number is equal to original number or not.

7. If it is equal, then print the output as "%d is a palindrome number.\n", otherwise print the output as "%d is not a palindrome number.\n".

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to reverse a number... >>
<< C Program to Print All Prime Numbers between 1 and...