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

43 Responses to “Java program to find whether no. is palindrome or not”

  1. Balakumar

    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)

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

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

    Reply
  4. 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;
    }
    }
    }

    Reply
  5. Amruta

    Am put that same code but i got the following error;;;
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at palindrome.main(palindrome.java:7)

    Reply
  6. Amruta

    i got the following error;;;
    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
    at palindrome.main(palindrome.java:7)

    Reply
  7. Profile photo of ram78

    import java.util.Scanner;

    public class Program {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);

    System.out.println(“Enter the number to be checked:”);
    int number=sc.nextInt();

    int num=number;

    int rev_num=0; //rev_num stands for reverse number
    while(num!=0){

    rev_num=rev_num*10 + num%10;
    num=num/10;

    }

    if(rev_num==number)
    System.out.println(“The number is a palindrome”);
    else
    System.out.println(“The number is not a palidrome “);

    }

    }

    ReplyReport user
  8. class palindrome
    {
    public static void main(String args[])
    {
    int n = 545, reverse = 0;
    int temp = n;

    while( n > 0 )
    {
    reverse = reverse * 10;
    reverse = reverse + n%10;
    n = n/10;
    }
    if(temp == reverse){
    System.out.println(“Number is Palindrome”);
    }else{
    System.out.println(“Number is not palindrome”);
    }
    }
    }

    Reply
  9. krishnateja

    import java.io.*;
    class palindrome1
    {
    public static void main(String args[])
    {
    //accept the string from Keyboard
    bufferedreaderbr.new bufferedread(Newinputstreamreader(System.in));
    System.out.printl(“enter a string:”);
    String str=br.readLine();
    //store a copy of original string intemp
    String temp=str;
    //convert the string int a string buffere string buffer sb= new string buffer(str);
    sb.reverse();
    //convert the string buffer into a string
    str=sb.tostring();
    //compare the original string available intemp with this reverse String if(temp equalslgnore case(str))
    System.out.println(temp+”is palindrome.”);
    else
    System.out.println(temp+”is not palindrome.”);
    }
    }

    Reply
  10. Sweta Kamilla

    package com;

    import java.util.Scanner;

    public class Palindrome
    {
    public static void main(String args[])
    {
    int r,sum=0,temp;

    Scanner sc=new Scanner(System.in);
    System.out.println(“enter the no”);
    int n=sc.nextInt();
    temp=n;
    while(n>0)
    {
    r=n%10;
    sum=(sum*10)+r;
    n=n/10;
    }

    if(temp==sum)
    System.out.println(“palindrome number “);
    else
    System.out.println(“not palindrome”);
    }
    }

    Reply

Leave a Reply