Palindrome Program in Python, C, C++, Java [Loop, Function, Recursion]

Palindrome Program in Python, C, C++, and Java [Beginner Guide]

Introduction

Palindrome numbers and strings are a common coding interview question asked in SRMU, BBD, Integral University, and many IT companies.
A palindrome is a number, string, or word that reads the same forward and backward.
Examples:

  • Number: 121, 1331
  • Word: madam, level, racecar

In this post, we will learn how to write a Palindrome Program in Python, C, C++, and Java using different approaches: Loop, Function, and Recursion.

Palindrome in Python

1. Using Loop

num = int(input("Enter a number: "))
temp = num
rev = 0

while num > 0:
    digit = num % 10
    rev = rev * 10 + digit
    num //= 10

if temp == rev:
    print("Palindrome")
else:
    print("Not Palindrome")

Using Function

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

n = int(input("Enter a number: "))
if is_palindrome(n):
    print("Palindrome")
else:
    print("Not Palindrome")

Using Recursion

def is_palindrome(s):
    if len(s) <= 1:
        return True
    if s[0] != s[-1]:
        return False
    return is_palindrome(s[1:-1])

s = input("Enter a string: ")
if is_palindrome(s):
    print("Palindrome")
else:
    print("Not Palindrome")

Palindrome in C

1. Using Loop


#include <stdio.h>

int main() {
int num, temp, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;

while (num > 0) {
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

if (temp == rev)
printf("Palindrome\n");
else
printf("Not Palindrome\n");

return 0;
}

2. Using Function

#include <stdio.h>

int isPalindrome(int num) {
    int temp = num, rev = 0, digit;
    while (num > 0) {
        digit = num % 10;
        rev = rev * 10 + digit;
        num /= 10;
    }
    return temp == rev;
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (isPalindrome(n))
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}

3. Using Recursion

#include <stdio.h>
#include <string.h>

int isPalindrome(char str[], int start, int end) {
    if (start >= end)
        return 1;
    if (str[start] != str[end])
        return 0;
    return isPalindrome(str, start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);

    if (isPalindrome(str, 0, strlen(str) - 1))
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}

Palindrome in C++

1. Using Loop

#include <iostream>
using namespace std;

int main() {
    int num, temp, rev = 0, digit;
    cout << "Enter a number: ";
    cin >> num;
    temp = num;

    while (num > 0) {
        digit = num % 10;
        rev = rev * 10 + digit;
        num /= 10;
    }

    if (temp == rev)
        cout << "Palindrome";
    else
        cout << "Not Palindrome";

    return 0;
}

2. Using Function

#include <iostream>
using namespace std;

bool isPalindrome(int num) {
    int temp = num, rev = 0, digit;
    while (num > 0) {
        digit = num % 10;
        rev = rev * 10 + digit;
        num /= 10;
    }
    return temp == rev;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    if (isPalindrome(n))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";

    return 0;
}

3. Using Recursion

#include <iostream>
using namespace std;

bool isPalindrome(string s, int start, int end) {
    if (start >= end)
        return true;
    if (s[start] != s[end])
        return false;
    return isPalindrome(s, start + 1, end - 1);
}

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;

    if (isPalindrome(str, 0, str.length() - 1))
        cout << "Palindrome";
    else
        cout << "Not Palindrome";

    return 0;
}

Palindrome in Java

1. Using Loop

import java.util.Scanner;

public class PalindromeLoop {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        int temp = num, rev = 0, digit;

        while (num > 0) {
            digit = num % 10;
            rev = rev * 10 + digit;
            num /= 10;
        }

        if (temp == rev)
            System.out.println("Palindrome");
        else
            System.out.println("Not Palindrome");

        sc.close();
    }
}

2. Using Function


import java.util.Scanner;

public class PalindromeFunction {
    static boolean isPalindrome(int num) {
        int temp = num, rev = 0, digit;
        while (num > 0) {
            digit = num % 10;
            rev = rev * 10 + digit;
            num /= 10;
        }
        return temp == rev;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        if (isPalindrome(n))
            System.out.println("Palindrome");
        else
            System.out.println("Not Palindrome");

        sc.close();
    }
}

3. Using Recursion


import java.util.Scanner;

public class PalindromeRecursion {
    static boolean isPalindrome(String str, int start, int end) {
        if (start >= end)
            return true;
        if (str.charAt(start) != str.charAt(end))
            return false;
        return isPalindrome(str, start + 1, end - 1);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = sc.next();

        if (isPalindrome(s, 0, s.length() - 1))
            System.out.println("Palindrome");
        else
            System.out.println("Not Palindrome");

        sc.close();
    }
}

Conclusion

Palindrome programs are frequently asked in university exams (SRMU, BBD, Integral) and coding interviews at companies.
We covered Python, C, C++, and Java solutions using Loop, Function, and Recursion.
👉 You should also check out:

Leave a Comment

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

Scroll to Top