INTRODUCTION
Write a program to Check whether the given number is a palindrome.
A palindrome number is a number that is as same as the original number when it is reversed.
Ex1:
Input: 12321
Output: palindrome number.
Ex2:
Input: 1234
Output: Not a Palindrome number.
APPROACH:
To check whether the given number is palindrome or not, we will check the number by reversing it and comparing it with the original number. If the number passes the above condition, then the number is treated as a palindrome number.
To reverse a number, we will extract the last digit by using the ‘%’ operator. After extracting the last digit. We will add to a temporary variable, let’s take it as a sum, by multiplying by 10. this iteration will be continued till the number is greater than zero.
If the temporary variable holds a value that is equal to the original number, then it is treated as a palindrome number, otherwise, it is not a palindrome number.
ALGORITHM:
Step 1: Start.
Step 2: Read an input number. Store it in a temporary variable, let’s take it as a temp.
Step 3: Extract the last digit of the number by using the ‘%’ operator.
Step 4: Add to a temporary variable, let’s take it as sum, by using the formulae sum=sum*10+last_digit.
Step 5: Check whether the original number and sum variable possess the same value or not by checking the temp variable and sum variable. If both are equal, then the number is a palindrome number. Otherwise, it is not a palindrome number.
Step 6: Stop.
C++ CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int temp = n,sum=0,digit=0;
while(n>0){
digit = n%10;
sum=sum*10+digit;
n = n/10;
}
if(sum == temp)
cout<<"Palindrome number";
else
cout<<"Not a Palindrome number";
}
JAVA CODE:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int temp = n,sum=0,digit=0;
while(n>0){
digit = n%10;
sum=sum*10+digit;
n = n/10;
}
if(sum == temp)
System.out.println("Palindrome number");
else
System.out.println("Not a Palindrome number");
}
}
PYTHON CODE:
n = int(input())
temp = n
sum=0
digit=0
while n>0:
digit = n%10
sum=sum*10+digit
n = n//10
if temp == sum:
print("Palindrome number")
else:
print("Not a Palindrome number")
Input 1: 12321
Output 1: Palindrome number.
Input 2: 1234
Output 2: Not a palindrome number.
The time complexity of the code is O(LogN).
The space complexity of the code is O(1).
NOTE:
A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 12321, and 123321 are palindrome numbers, while 124, 1235, and 1236 are not. In general, any number that reads the same forwards and backward is a palindrome number.
There is a unique method in python that helps us to reverse the number directly. So, we can reverse the number and can check whether the given number qualifies for the palindrome condition. This method is known as reversing.
Let’s see the code.
PYTHON CODE:
n = str(int(input()))
temp = n
if n[::-1] == temp:
print("Palindrome number")
else:
print("Not a Palindrome number")
Input 1: 12321
Output 1: Palindrome number.
Input 2: 1234
Output 2: Not a palindrome number.
In the above program, we have used the reversing method to reverse the given string which we had taken as input. After reversing, we checked the given number with the original number. If the number satisfies the palindrome condition, then it is treated as a palindrome number. Otherwise, it is not a palindrome number.
CONCLUSION
That’s it from this tutorial. Hope you guys found It interesting. We have solved the problem to check whether the given number is palindrome or not in different programming languages by using different numbers. If you find any difficulties, please leave a comment below. Happy coding!