Question: Create a Program that prints all digits of any integer in reverse order. import java.util.Scanner; public class ReverseDigits { public static void main(String[] args)

Create a Program that prints all digits of any integer in reverse order.

import java.util.Scanner;
public class ReverseDigits
{
  public static void main(String[] args)
  {
     Scanner in = new Scanner(System.in);
     int n = in.nextInt();
     // TODO: Print the digits of n in reverse

     /* Your code goes here */
       System.out.print("");
       if (n < 0)
       {
          /* Your code goes here */
          n = n*-1; //Convert number to a positive value
          System.out.print(""); //Print negative 
       }
       else if (n == 0)
       {
           /* Your code goes here */
           System.out.println("0");
           return;
       }
       
       int reverseNumber = 0;
       while (n != 0)
       {
           int digit = n % 10; //extracting the last digit from n
           
           reverseNumber = reverseNumber*10 + digit; // printing the digit
           
           n = n / 10; //updating n by removing the last digit
       }
       
       System.out.print(reverseNumber);

 


     System.out.println();
  }
}

 

Not all tests passed.

check1: Compare outputkeyboard_arrow_up

 

Input

7

 

Your output

7

 

clear2: Compare outputkeyboard_arrow_up

 

Output differs. See highlights below.

 

Input

-1234

 

Your output

4321

 

Expected output

4321-

 

check3: Compare outputkeyboard_arrow_up

 

Input

0

 

Your output

0

 

check4: Compare outputkeyboard_arrow_up

 

Input

112358

 

Your output

853211

 

check5: Compare outputkeyboard_arrow_up

 

Input

1729

 

Your output

9271


Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the description provided it seems that the program is intended to print all the digits of a... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!