C programming

To check it is PALINDROME or not using C | C program

To check it is PALINDROME or not : To check it is PALINDROME or not in c program first you have to know what is PALINDROME ?To Print it is PALINDROME or not you have to clear your logic.Lets begin,

What is palindrome?

  • When a number or word or a phrase or a sequence of characters which resembles the same while reading them from backward are called palindrome.
  • Palindrome examples: 2882, madam, noon, level.
  • The above examples resemble as same while reading them from backward also. If you read number “2882” from backward, it is same as “2882”. If you read the word “madam” from backward, it is same as “madam”

C Program to check a number if it is PALINDROME or not:

#include <stdio.h>
int main()
{
   int number, t, rev=0, rmndr;

   printf("Please enter a number to check Palindrome : ");
   scanf("%d",&number);
   printf("\nEntered number: %d", number);
 
   t = number;

   while (number > 0)
   {
      rmndr = number%10;
      rev = rev*10 + rmndr;
      number = number/10;
   }
   printf("\nReversed number: %d", rev);

   if(t == rev)
   {
      printf("\nEntered number %d is a palindrome", t);
   }
   else
   {
      printf("\nEntered number %d is not a palindrome", t);
   }
   return 0;
}

OUTPUT:

Please enter a number to check Palindrome : 656
Entered number: 656
Reversed number: 656
Entered number 656 is a palindrome

C Program to check a word if it is PALINDROME or not:

#include <stdio.h>
#include <string.h>
int main()
{
   char str[], i=0;
   int str_len;

   printf("Please enter a word to check Palindrome : ");
   scanf("%s",str);
   printf("\nEntered word: %s", str);

   while(1)
   {
      str_len = strlen(str) - (i+1);

      if (str[i] == str[str_len])
      {
         if (i == str_len || i+1 == str_len)
         {
            printf("\nEntered word \"%s\" is a palindrome", str);
            break;
         }
         i = i+1;
      }
      else
      {
         printf("\nEntered word \"%s\" is not a palindrome", str);
         break;
      }
   }
   return 0;
}

OUTPUT :

Please enter a word to check Palindrome : madam
Entered word: madam
Entered word “madam” is a palindrome

Leave a Reply

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

40 − = 31