Create a Program that prints all digits of any integer in reverse order. import java.util.Scanner; public class
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)
{
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
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang