Here’s a Java program to reverse a number:
import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { int num, reversedNum = 0; Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); num = scanner.nextInt(); while (num != 0) { int digit = num % 10; reversedNum = reversedNum * 10 + digit; num /= 10; } System.out.println("Reversed number: " + reversedNum); } }
In this program, we first take user input for a number. We then use a while loop to iterate over each digit of the number. In each iteration, we use the remainder operator `%` to get the last digit of the number and add it to the `reversedNum` variable. We then multiply `reversedNum` by 10 and add the last digit to it to shift the digits one place to the left. We then divide the number by 10 to remove the last digit. We repeat this process until the number becomes 0.
At the end of the program, we print out the reversed number.