BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to check if a number is palindrome or not

Last Updated: February 11, 2015 by Chaitanya Singh | Filed Under: C Programs

If a number remains same, even if we reverse its digits then the number is known as palindrome number. For example 12321 is a palindrome number because it remains same if we reverse its digits. In this article we have shared two C programs to check if the input number is palindrome or not. 1) using while loop 2) using recursion.

Program 1: check palindrome using while loop

/* 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;
}

Output:
checking_palindrome_number_output

Program 2: check palindrome using recursion

#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;
}

Output:
palindrome_using_recursion

Top Related Articles:

  1. C Program to check if number is even or odd
  2. C Program to find sum of array elements
  3. C Program to concatenate two strings without using strcat
  4. C Program to display factors of a number
  5. C Program to Find ASCII value of a Character

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Mamun Paul says

    June 30, 2017 at 2:52 PM

    It can be solved with less v
    Variable
    Like:
    main()
    {
    into num,ori;
    clrscr();
    printf(” \n Enter any four digit integer no.:”);
    scanf(“%d”, & num);
    ori= num;
    num=num%10*1000+num/10%10*100+num/10%10*10+num/1000;
    if(ori==num)
    {
    printf(“\n no. Is palindrome”);
    }
    else
    {
    printf(“\n no. Is not palindromes”);
    }
    getch();
    }

    Reply

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap