Program to Find Whether a Number is a Palindrome or Not [C, C++, Python & Java]

Posted in

Program to Find Whether a Number is a Palindrome or Not [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    In this tutorial, we will learn how to write a program in C, C++, Python, and Kava that checks whether the entered number is a palindrome or not.

    Here are the prerequisites to writing a palindrome program:

    • while loop
    • if...else statement

    What is a Palindrome Number?

    A palindrome number is a unique number whose reverse is equal to it. In other words, a palindrome is a number that remains the same even if it is reversed.

    For example, 11, 111, 121, 131… are palindrome numbers.

    Palindrome Program in C

    #include <stdio.h>
    int main()
    {
        int num, temp, remainder, reserved=0;
        printf("Enter a number: ");
        scanf("%d", &num);
        temp = num;
        while (num!=0)
        {
            remainder = num%10;
            reserved = reserved*10+remainder;
            num = num/10;
        }
        if (temp == reserved)
            printf("%d is a palindrome.", temp);
        else
            printf("%d is not a palindrome.", temp);
        return 0;
    }

    Output:

    Enter a number: 454
    454 is a palindrome.

    Palindrome Program in C++

    #include<iostream>
    using namespace std;
    int main()
    {
       int num, temp, remainder, reserved=0;
       cout<<"Enter a number: ";
       cin>>num;
       temp = num;
       while(num!=0)
       { 
          remainder = num%10;
          reserved = reserved*10+remainder;
          num = num/10;
       }
       if(temp == reserved)
          cout<<temp<<" is a palindrome";
       else
          cout<<temp<<" is not a palindrome";
       return 0;
    }

    Output:

    Enter a number: 656
    656 is a palindrome

    Palindrome Program in Python

    num =int(input("Enter a Number: "))
    temp = num
    reserved =0
    while num!=0:
        remainder = num%10;
        reserved = reserved*10+remainder;
        num = num//10;
        
    if temp == reserved:
        print(temp, "is a palindrome")
    else:
        print(temp, "is not a palindrome")

    Output:

    Enter a Number: 1111
    1111 is a palindrome

    Palindrome Program in Java

    import java.util.*;
    public class Main
    {
       public static void main(String[] args)
       { 
          int num, temp, remainder, reserved=0;
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a number: ");
          num = sc.nextInt();
          temp = num;
          while(num!=0)
          { 
             remainder = num%10;
             reserved = reserved*10+remainder;
             num = num/10;
          }
       if(temp == reserved)
          System.out.println(+temp+ " is a palindrome");
       else
          System.out.println(+temp+ " is not a palindrome");
      
       }
    }

    Output:

    Enter a number: 
    676
    676 is a palindrome

    Conclusion

    We hope the above programs in C, C++, Python, and Java have helped you understand the concept of a palindrome number. Once you understand the logic of separating the digits of a number, you can easily implement a palindrome program.

    Try it on your own!

    People are also reading:

    Leave a Comment on this Post

    0 Comments