aster
aster

Reputation: 83

how to reverse number in 4 digits(java)

This is my code down below;

 int reverse = 0;

     while( num != 0 )
      {
      reverse = reverse * 10;
      reverse = reverse + num%10;
      num = num/10;

    }

    System.out.println(reverse);

if i input :1234 it will show 4321

but if i input 0044

it will output 44

i want to output 4400

what should i type in?

Upvotes: 1

Views: 4991

Answers (5)

Mohammad
Mohammad

Reputation: 6148

If you want to include "0044" and similar cases you should look at the input as a String. Whenever you need the number you can convert it to Integer. Try this:

public class ReverseNumber {

    static int reverse = 0;

    public static void main(String[] args) {

        String num = "0044";
        String temp = new StringBuilder(num).reverse().toString();
        reverse = Integer.valueOf(temp);
        System.out.println(reverse);

    }

}

I hope it helps.

Upvotes: 0

persistent_poltergeist
persistent_poltergeist

Reputation: 564

You might have to try working with string to achieve this. try the code below

 import java.util.Scanner;
 public class Reverse 
  {

   public static void main(String[] args) 
   {

     Scanner in = new Scanner(System.in);
     System.out.print("Enter a number: ");
     StringBuilder number=new StringBuilder(in.next());
     System.out.println(number.reverse());
   }
 }

Hope this helps :)

now if you want to find the difference between the reversed number and the input number

 import java.util.Scanner;

 public class Reverse {

  public static void main(String[] args) {
    int a;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a number: ");
    String number=in.next();
    StringBuilder numbertoRev=new StringBuilder(number);
    try{
    int a=Integer.valueOf(number);
    }catch(NumberFormatException e)
    {
       System.out.println("not a number :)");
       return;
    }
    int b=Integer.valueOf(numbertoRev.reverse().toString());
    System.out.println(b-a);
}

This is what i understood from your question. Pardon me if this is not what you are looking for :)

Upvotes: 0

Andreas
Andreas

Reputation: 159125

You're almost there, you just need to loop exactly 4 times. You should also guard against bad data, and you should print number as 4 digits:

if (num < 0 || num > 9999)
    throw new IllegalArgumentException("Not a 4-digit number: " + num);
int reverse = 0;
for (int i = 0; i < 4; i++, num /= 10)
    reverse = reverse * 10 + num % 10;
System.out.printf("%04d%n", reverse);

Of course, you could also do it using String manipulation:

if (num < 0 || num > 9999)
    throw new IllegalArgumentException("Not a 4-digit number: " + num);
String reverse = new StringBuilder(String.format("%04d", num)).reverse().toString();
System.out.printf(reverse);

Both methods can be enhanced to support 5+ digits if needed.

Followup

Comment in answer by Ninth-tail:

if i want 4321(reverse number) - 1234(input number) what should i do?

The first version above is best for that, since you already have the numbers, with minor change to retain original input:

if (input < 0 || input > 9999)
    throw new IllegalArgumentException("Not a 4-digit number: " + input);
int reverse = 0;
for (int i = 0, num = input; i < 4; i++, num /= 10)
    reverse = reverse * 10 + num % 10;
System.out.printf("Reverse: %04d%n", reverse);
System.out.printf("Difference: %04d%n", reverse - input);

Upvotes: 0

mksteve
mksteve

Reputation: 13085

The issue is your loop size, not your logic. Assuming all your numbers are 4 digits, then

 int reverse = 0;

 int i;
 for( i = 0; i < 4 ; i++ )
  {
  reverse = reverse * 10;
  reverse = reverse + num%10;
  num = num/10;

}

System.out.println(reverse);

will work

Upvotes: 1

Eran
Eran

Reputation: 393906

Since leadig zeroes have no meaning in an int, you'll have to read your input as a String and reverse that String in order to keep the leading zeroes (and turn them into trailing zeroes).

Upvotes: 3

Related Questions