[03 Feb 2020]Program to check the number is Palindrome or not


Program to check the number is Palindrome or not
Given an integer N, write a program that returns true if the given number is a palindrome, else return false.

Examples:

Input: N = 2002
Output: true

Input: N = 1234
Output: false
#include <stdio.h>

/* Iterative function to reverse digits of num*/
int reverseDigits(int num)
{
    int rev_num = 0;
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}

/* Function to check if n is Palindrome*/
int isPalindrome(int n)
{

    // get the reverse of n
    int rev_n = reverseDigits(n);

    // Check if rev_n and n are same or not.
    if (rev_n == n)
        return 1;
    else
        return 0;
}

/*Driver program to test reversDigits*/
int main()
{
    int n = 4562;
    printf("Is %d a Palindrome number? -> %s\n", n,
           isPalindrome(n) == 1 ? "true" : "false");

    n = 2002;
    printf("Is %d a Palindrome number? -> %s\n", n,
           isPalindrome(n) == 1 ? "true" : "false");
    return 0;
}
Output:
Is 4562 a Palindrome number? -> false
Is 2002 a Palindrome number? -> true

filter_none
edit
play_arrow

brightness_4
# Python3 program to check whether a
# number is Palindrome or not.

# Iterative function to reverse
# digits of num
def reverseDigits(num) :

    rev_num = 0;
    while (num > 0) :
        rev_num = rev_num * 10 + num % 10
        num = num // 10
   
    return rev_num

# Function to check if n is Palindrome
def isPalindrome(n) :

    # get the reverse of n
    rev_n = reverseDigits(n);

    # Check if rev_n and n are same or not.
    if (rev_n == n) :
        return 1
    else :
        return 0

# Driver Code
if __name__ == "__main__" :

    n = 4562
   
    if isPalindrome(n) == 1 :
        print("Is", n, "a Palindrome number? ->", True)
       
    else :
        print("Is", n, "a Palindrome number? ->", False)

    n = 2002
   
    if isPalindrome(n) == 1 :
        print("Is", n, "a Palindrome number? ->", True)
       
    else :
        print("Is", n, "a Palindrome number? ->", False)

# This code is contributed by Ryuga
Output:
Is 4562 a Palindrome number? -> false
Is 2002 a Palindrome number? -> true


// Recursive C++ program to check if the  number is palindrome or not 

#include <bits/stdc++.h>
using namespace std;

// recursive function that returns the reverse of digits
int rev(int n, int temp)
{
    // base case
    if (n == 0)
        return temp;

    // stores the reverse of a number
    temp = (temp * 10) + (n % 10);

    return rev(n / 10, temp);

// Driver Code
int main()
{
    int n = 121;     
    int temp = rev(n, 0);   
    if (temp == n)
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    return 0;
}
Output:
yes


# Recursive Python3 program to check
# if the number is palindrome or not

# Recursive function that returns
# the reverse of digits
def rev(n, temp):

    # base case
    if (n == 0):
        return temp;

    # stores the reverse of a number
    temp = (temp * 10) + (n % 10);

    return rev(n / 10, temp);

# Driver Code
n = 121;
temp = rev(n, 0);

if (temp != n):
    print("yes");
else:
    print("no");

# This code is contributed
# by mits
Output:
yes

Comments

Popular posts from this blog

Kth most frequent Character in a given String

[16 Feb 2020] Given an array where every element occurs three times, except one element which occurs only once.

[17 Feb 2020] Given an array of integers, find the nearest smaller number for every element such that the smaller element is on left side.