1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| /* Write a program to find whether no. is palindrome or not.
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
class Palindrome{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
}
} |
/* Write a program to find whether no. is palindrome or not.
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
class Palindrome{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
}
}
thank u ….
Here is another way to check palindrome in Java
its superb
Plz dont post adds on main content.
It is too worse….
Thanks
Also refer this for PALINDROME OF A NUMBER AND A STRING IN SAME USER INPUT
http://knowitenjoyit.blogspot.in/2013/03/palindrome-number-string-java.html
the while loop should be while(num>=1)
because during the last check up it will give a value in point
kuch samaj me he nahi aaya yar…. nya nya hu na is liye 🙁 :'(
thanks …
I THING IT IS WRONG
LET N=121
THEN
REMAINDER=1
REVES=1
N=N /10=12
N=12
REMAINDER=2
REV=2*10+1=21
N=N/10=1
N=1
REMAINDER=1%10=1
REV=1*10+21=31
WHICH IS WRONG..
I THING IT IS WRONG
LET N=121
THEN
REMAINDER=1
REVES=1
N=N /10=12
N=12
REMAINDER=2
REV=2*10+1=21
N=N/10=1
N=1
REMAINDER=1%10=1
REV=1*10+21=31
WHICH IS WRONG..
OR I AM UNABLE TO UNDERSTAND
no it is right
suppose=121
then
remainder=121%100=1
reverse=0*10+1=1
num=121 /10=12
now num=12
remainder=12%10=2
reverse=1*10+2=12 //because remainder is still 1
num=12/10=1
now n=1
remainder=1%10=1
reverse=12*10+1=121
num=1/10=0
So that is true 🙂
what is the use of this post..
dont post adds on main content
thank you so much
I easily understood.
once again thanks
getting the Following error::
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at Palindrome.main(Palindrome.java:4)
Sir apka pair chune ka man kr rha h!
aap nhi hote ho hm aaj engineer na hote,,,,
madarchod pura kam aya external exam me….
kyaaasaaaab????
upar jo jo comments kia h, apne apne maa chudao!
kyasaaaaaab?????
Two versions, check this > http://www.msccomputerscience.com/2014/05/java-program-to-check-whether-string-is.html
you hav to pass a value at the time of execution through command prompt
Am put that same code but i got the following error;;;
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at palindrome.main(palindrome.java:3)
the given code is showing error in ‘else’ condition.
class palindrome
{
public static void main (string[]args)
{
int n=0,digit,r=0;
int num=141;
do
{
digit=num%10;
r=(r*10)+digit;
num = num%10;
}
while(num ! =0);
System.out.println(“the reverse of number is :”+r);
if(num==r)
{
System.out.println(“the number is palindrome.”);
}
else
{
System.out.println(“the number is not palindrome.”);
}
}
}
try this one .
import java.util.Scanner;
class Palindrome
{
public void main(String args[])
{
int temp,sum=0,sum1;
Scanner userInputScanner=new Scanner(System.in);
System.out.println(“ENTER ANY NUMBER:-“);
int number=userInputScanner.nextInt();
while(number>0)
{
temp=number%10;
sum=(sum*10)+number;
number=number/10;
}
if(number==sum1)
System.out.println( ” IS AN PALINDROME NUMBER”);
else
System.out.println( ” IS AN NOT PALINDROME NUMBER”);
}
}
output:-variable sum may not be initialized
can any one help him out of this with reason
i tried your program there’s an error when i tried to run it. it says variable sum1 might not have been initialized
http://wp.me/p5BnfA-9u
TRY THIS
http://wp.me/p5BnfA-9u
TRY THIS
int number=userInputScanner.nextInt();
after this statement include this line: sum1=number;
n it will b done..
wanted how does it work
This program is with the Scanner
import java.util.Scanner;
class Fibonacci
{
public static void main(String args[])
{
System.out.println(“Enter the maximum no. of lines”);
Scanner ob=new Scanner(System.in);
int ch = ob.nextInt();
System.out.println(“Ther you go with the “+ch+” series of Fibanocci Numbers”);
int a, b, s, n;
a=b=1;
for(n=1;n<=ch;n++)
{
System.out.println(a);
s=a+b;
a=b;
b=s;
}
}
}
u run program…
java Palindrome 12321
check Simple and easy program for palindrome here http://www.javaengineeringprograms.com/program-to-check-palindrome-in-java/
hey check this program http://goo.gl/M32KuF
Learn more about java tutorial special for beginners
http://www.javascode.com
check this Simple and Short program http://goo.gl/M32KuF