0

This might be a very dumb question. I tried to reverse the input number and compare it.If they are same then, the output should be "the number entered is a palindrome" But, I'm getting out for every number like it is a palindrome.

package com.practise.examples;

import java.util.Scanner;

public class Practise 
{
    public static void main(String[] args) {


        Scanner s=new Scanner(System.in);
        System.out.println("enter the number to reverse it:\n");
        int num=s.nextInt();
        int revNum=0;

        while(num!=0)
        {
            revNum=revNum *10;
            revNum= revNum+ num%10;
            num=num/10;
        }
        System.out.println("the reverse of the number is: " +revNum);

            if(revNum==num)
            {
                System.out.println("the number is a palindrome" );

            }
            else
            {
                System.out.println("the number entered is not a palindrome");

            }

        }
    }
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
BeginnerJava
  • 11
  • 1
  • 1
  • Note that you're destroying `num` here anyway, so `num == 0` when you compare `revNum == num`. – Andy Turner Jul 05 '17 at 14:12
  • 1
    You shouldn't change `num` when creating the reverse number, otherwise you'll lose the orignal number. – Thomas Jul 05 '17 at 14:12
  • It's hard to compare the original number to the reversed number if the original number keeps changing. – Compass Jul 05 '17 at 14:12

5 Answers5

4

Easier way:

String num=Integer.toString(s.nextInt());
String revNum = new StringBuffer(num).reverse().toString();
System.out.println("the reverse of the number is: " +revNum);

if(revNum.equals(num))
    System.out.println("the number is a palindrome" );
else
    System.out.println("the number entered is not a palindrome");

If you're insisting on your method:

    Scanner s=new Scanner(System.in);
    System.out.println("enter the number to reverse it:\n");
    int num=s.nextInt();
    int original = num;
    int revNum=0;

    while(num!=0)
    {
        revNum=revNum *10;
        revNum= revNum+ num%10;
        num=num/10;
    }
    System.out.println("the reverse of the number is: " +revNum);

        if(revNum==original)
        {
            System.out.println("the number is a palindrome" );

        }
        else
        {
            System.out.println("the number entered is not a palindrome");

        }

    }
omerfarukdogan
  • 839
  • 9
  • 26
1

Try something like this:

public static int reverse(int num)
{
  try
  {
    return Integer.parseInt(new StringBuilder(String.valueOf(num)).reverse().toString());
  }
  catch (Exception ex)
  {
    // Should not happen...
  }
}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

You have modified num for getting the reverse, and then using the same num for comparison. Use temporary variables.

Isha Agarwal
  • 420
  • 4
  • 12
0

your idea seems to only work in VERY specific instances, and the code itself... well, ill just give you a different idea.

create a stack and a queue.

take the original input, and add each consecutive item to each the stack and the queue.

once the queue and stack are full...

iterate until length=0
if stack.pop != queue.dequeue
    palindrome=false
Jason V
  • 623
  • 1
  • 10
  • 30
0

you can cast your number to a string and check doe's its palindrome?

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        System.out.println("enter the number to reverse it:\n");
        int num=s.nextInt();
        if (isPalindrome(num+"")){
            System.out.println("the number is a palindrome" );
        }else{
            System.out.println("the number entered is not a palindrome");
        }
    }
    public static boolean isPalindrome(String s) {
        int n = s.length();
        for (int i = 0; i < (n/2); ++i) {
            if (s.charAt(i) != s.charAt(n - i - 1)) {
                return false;
            }
        }

        return true;
    }
}
Ebrahim Poursadeqi
  • 1,776
  • 2
  • 17
  • 27