Answer to Question #328436 in Java | JSP | JSF for rose

Question #328436

Write a java code that prompts the user to input a non-negative integer and then it will reverse the same number using a while loop. For example, if the input number is 25 the output should be 52.

Sample input and output:

Example 1:

Input: 25

Output: 52

Example 2:

Input: 123

Output: 321

Example 3:

Input: 3

Output: 3


1
Expert's answer
2022-04-13T13:17:21-0400
import java.util.*;
public class App {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int num = 1234, reversed = 0;
        System.out.print("Enter number: ");
        num = keyboard.nextInt();
        while(num != 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }
        System.out.println("Reversed Number: " + reversed);
        keyboard.close();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS