Write a Java program to input three-digit integer number and reverse
it.
Answers
Explanation:
Reverse a number in Java
class ReverseNumber. { public static void main(String args[]) { int n, reverse = 0;
System. out. println("Enter an integer to reverse"); Scanner in = new Scanner(System. in); n = in. nextInt();
while(n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; }
This program can also reverse numbers having more than 3 digits
import java.util.Scanner;
public class NumberReverser {
int reverse = 0;
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
StringBuilder number = new StringBuilder(sc.next());
System.out.print("The reversed number is ");
System.out.print(number.reverse());
}
}
Output:
Enter a number: 123456789
The reversed number is 987654321