Java program to find whether no. is palindrome or not

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");
	}
}
surajk

38 thoughts on “Java program to find whether no. is palindrome or not

  1. the while loop should be while(num>=1)
    because during the last check up it will give a value in point

  2. 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..

  3. 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..

  4. 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 🙂

  5. getting the Following error::
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at Palindrome.main(Palindrome.java:4)

  6. 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????

  7. 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)

  8. 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.”);
    }
    }
    }

  9. 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

  10. int number=userInputScanner.nextInt();

    after this statement include this line: sum1=number;
    n it will b done..

  11. 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;
    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in