C

Palindrome in C

In this tutorial, we are going to see how to check if a number is a palindrome in C. A number is a palindrome if it is written the same way after reversing it.
 
Examples:

232, 191, 22022, 111, 666, etc.

 

Program Logic
  • Get the number to check
  • Keep the number in a temporary variable
  • Reverse the number
  • Compare the temporary number with the reversed number
  • If the two numbers are the same, display “the number is a palindrome”
  • Otherwise, display “the number is not a palindrome”

 

 

Program to check if a number is a Palindrome in C:
#include <stdio.h>
 
int main()
{
   int nbr, tmp, reversedNbr = 0;
 
   printf("Enter a number to check if it is a palindrome or not: \n");
   scanf("%d", &nbr);
 
   tmp = nbr;
 
   while (tmp != 0)
   {
      reversedNbr = reversedNbr * 10;
      reversedNbr = reversedNbr + tmp%10;
      tmp = tmp/10;
   }
 
   if (nbr == reversedNbr)
      printf("%d is a palindrome number.\n", nbr);
   else
      printf("%d is not a palindrome number.\n", nbr);
 
   return 0;
}

Output:

Enter a number to check if it is a palindrome or not: 
121
121 is a palindrome number.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *