Program to Check if Given Number is Palindrome or Not using Recursion

Program to Check if Given Number is Palindrome or Not using Recursion

  • Write a program to Check if Given Number is Palindrome or Not using Recursion in C
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in C++
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in Python
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in PHP
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in Java
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in Java Script
  • Write a program to Check if Given Number is Palindrome or Not using Recursion in C#

Explanation:

In order to use recursion to determine whether a given number is a palindrome, we must first compare the number’s first and last digits before recursively checking the remaining digits, ignoring the first and last. A number is considered a palindrome if it remains the same when read both forward and backward.

Steps:

  1. Base Case:
    • If the number has only one digit or is empty (i.e., n == 0), return True because single-digit numbers are always palindromes.
    • If the number has two digits, check if the first digit is equal to the last digit. If they are equal, return True; otherwise, return False.
  2. Recursive Case:
    • Extract the first and last digits of the number.
    • Remove the first and last digits by dividing and taking the remainder.
    • Recurse with the remaining number after removing the first and last digits.
  3. Stopping Condition:
    • The recursion stops once all the digits are compared and found to be the same.

Program to Check if Given Number is Palindrome or Not using Recursion

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-c">#include <stdio.h>

int check_palindrome(int num, int rev, int original) {
    if (num == 0)
        return (rev == original) ? 1 : 0;
    
    rev = rev * 10 + num % 10;
    return check_palindrome(num / 10, rev, original);
}

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

    int result = check_palindrome(num, 0, num);
    
    if (result)
        printf("%d is a palindrome.\n", num);
    else
        printf("%d is not a palindrome.\n", num);

    return 0;
}

#include <iostream>
using namespace std;

int check_palindrome(int num, int rev, int original) {
    if (num == 0)
        return (rev == original) ? 1 : 0;

    rev = rev * 10 + num % 10;
    return check_palindrome(num / 10, rev, original);
}

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

    int result = check_palindrome(num, 0, num);

    if (result)
        cout << num << " is a palindrome." << endl;
    else
        cout << num << " is not a palindrome." << endl;

    return 0;
}

def check_palindrome(num, rev, original):
    if num == 0:
        return rev == original
    rev = rev * 10 + num % 10
    return check_palindrome(num // 10, rev, original)

num = int(input("Enter a number: "))
if check_palindrome(num, 0, num):
    print(f"{num} is a palindrome.")
else:
    print(f"{num} is not a palindrome.")

<?php
function check_palindrome($num, $rev, $original) {
    if ($num == 0)
        return $rev == $original;
    
    $rev = $rev * 10 + $num % 10;
    return check_palindrome(intval($num / 10), $rev, $original);
}

echo "Enter a number: ";
$num = intval(trim(fgets(STDIN)));

if (check_palindrome($num, 0, $num))
    echo "$num is a palindrome.\n";
else
    echo "$num is not a palindrome.\n";
?>

import java.util.Scanner;

public class Main {
    public static boolean check_palindrome(int num, int rev, int original) {
        if (num == 0)
            return rev == original;
        rev = rev * 10 + num % 10;
        return check_palindrome(num / 10, rev, original);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (check_palindrome(num, 0, num))
            System.out.println(num + " is a palindrome.");
        else
            System.out.println(num + " is not a palindrome.");
    }
}

function check_palindrome(num, rev, original) {
    if (num === 0)
        return rev === original;
    rev = rev * 10 + num % 10;
    return check_palindrome(Math.floor(num / 10), rev, original);
}

const num = parseInt(prompt("Enter a number:"));
if (check_palindrome(num, 0, num))
    console.log(`${num} is a palindrome.`);
else
    console.log(`${num} is not a palindrome.`);

using System;

class Program {
    static bool CheckPalindrome(int num, int rev, int original) {
        if (num == 0)
            return rev == original;
        rev = rev * 10 + num % 10;
        return CheckPalindrome(num / 10, rev, original);
    }

    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());

        if (CheckPalindrome(num, 0, num))
            Console.WriteLine($"{num} is a palindrome.");
        else
            Console.WriteLine($"{num} is not a palindrome.");
    }
}

List of All Programs