Python/C/C++/JAVA

Basic Practice Programs with Code and Concept

By D.S

Palindrome Numbers

Palindrome numbers are fascinating numerical phenomena that are the same when read forwards or backwards. They're like numerical versions of word palindromes, such as "racecar" or "level." Palindromic numbers can be found in various contexts, from natural occurrences to mathematical puzzles, and even in everyday life, like birth dates or phone numbers. Let's explore the world of palindrome numbers together!

(a.) C Code

#include <stdio.h>

    int main() {
        int num, reversedNum = 0, originalNum, remainder;

        printf("Enter an integer: ");
        scanf("%d", &num);

        originalNum = num;

        // Reversing the number
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }

        // Checking if the number is palindrome
        if (originalNum == reversedNum)
            printf("%d is a palindrome.
", originalNum);
        else
            printf("%d is not a palindrome.
", originalNum);

        return 0;
    }
Output:-
Enter an integer: 121
121 is a palindrome.

(b.) C++ Code

#include <iostream>
    using namespace std;

    int main() {
        int num, reversedNum = 0, originalNum, remainder;

        cout << "Enter an integer: ";
        cin >> num;

        originalNum = num;

        // Reversing the number
        while (num != 0) {
            remainder = num % 10;
            reversedNum = reversedNum * 10 + remainder;
            num /= 10;
        }

        // Checking if the number is palindrome
        if (originalNum == reversedNum)
            cout << originalNum << " is a palindrome." << endl;
        else
            cout << originalNum << " is not a palindrome." << endl;

        return 0;
    }
Output:-
Enter an integer: 121
121 is a palindrome.

(c.) Python Code

def is_palindrome(num):
        return str(num) == str(num)[::-1]

    num = int(input("Enter an integer: "))
    if is_palindrome(num):
        print(num, "is a palindrome.")
    else:
        print(num, "is not a palindrome.")
Output:-
Enter an integer: 121
121 is a palindrome.

(d.) Java Code

import java.util.Scanner;

    public class PalindromeCheck {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter an integer: ");
            int num = scanner.nextInt();

            int reversedNum = 0, originalNum = num, remainder;

            // Reversing the number
            while (num != 0) {
                remainder = num % 10;
                reversedNum = reversedNum * 10 + remainder;
                num /= 10;
            }

            // Checking if the number is palindrome
            if (originalNum == reversedNum)
                System.out.println(originalNum + " is a palindrome.");
            else
                System.out.println(originalNum + " is not a palindrome.");
        }
    }
Output:-
Enter an integer: 121
121 is a palindrome.