C

C Program To Determine Whether Or Not A Number Is A Palindrome

A palindrome number is one that remains the same even when its digits are reversed. For example, the number 12321 is a palindrome because it remains the same when its digits are reversed. In this article, we shared two C programs for determining whether or not an input number is a palindrome. 1) employing a while loop 2) employing recursion

Program 1: use a while loop to check for palindromes

/* Program to check if a number is palindrome or not
 * using while loop
 */

#include <stdio.h>
int main()
{
   int num, reverse_num=0, remainder,temp;
   printf("Enter an integer: ");
   scanf("%d", &num);

   /* Here we are generating a new number (reverse_num)
    * by reversing the digits of original input number
    */
   temp=num;
   while(temp!=0)
   {
      remainder=temp%10;
      reverse_num=reverse_num*10+remainder;
      temp/=10;
   } 

   /* If the original input number (num) is equal to
    * to its reverse (reverse_num) then its palindrome
    * else it is not.
    */ 
   if(reverse_num==num) 
      printf("%d is a palindrome number",num);
   else
      printf("%d is not a palindrome number",num);
   return 0;
}

Program 2: Use recursion to check for palindromes

#include<stdio.h>

int check_palindrome(int num){

   static int reverse_num=0,rem;

   if(num!=0){
      rem=num%10;
      reverse_num=reverse_num*10+rem;
      check_palindrome(num/10);
   }

   return reverse_num;
}
int main(){
   int num, reverse_num;

   printf("Enter a number: ");
   scanf("%d",&num);

   reverse_num = check_palindrome(num);

   if(num==reverse_num)
      printf("%d is a palindrome number",num);
   else
      printf("%d is not a palindrome number",num);

   return 0;
}

 

Related Articles

Leave a Reply

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

Back to top button