Number Palindrome in java
Checking Number Palindrome
What is Palindrome ?
> A palindrome is number,string or phrase which reads same as backward and forward. Ex-MADAM, 121 etc.
Steps to do this:-
1. take 4 a,r,sum=0,integers ( a is for user input number, r is for to store remainder , sum=0 is for performing addition task [we have to initialize it otherwise it will store garbage value and our code will be messed :( ] t is for to store the user input number for checking )2. take a scanner class and make an object and scan the user input number
3. now store the number in t
t=a
for further use
so in here we are just finding the reverse of the number , so if you not watch my blog that how to reverse a number then click here
4. start the while loop from (a>0) and do this
while(a>0)
{
r=a%10;// it will store the last number of the digit
sum=sum*10+r;// it will perform the digit addition
a=a/10; // it will eliminate the last number and stores the rest of the numbers
}
note* for detail explanation check my blog that how to reverse a number by clicking the link
5. now start the if condition and check
if(sum==t)
{
print " palindrome"
else
{
print "not palindrome ";
}
when,the reverse is equal to the number then it will perform the if block otherwise it will perform the else block
6. save and run
The Source Code
import java.util.Scanner;class numberpali
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a,t,r,sum=0;
System.out.println("enter the number");
a=s.nextInt();
t=a;
while (a>0)
{
r=a%10;
sum=sum*10+r;
a=a/10;
}
if(sum==t)
{
System.out.println("this is palindrome");
}
else
{
System.out.println("this is not a palindrome");
}
}
}
OUTPUT
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know