Java program to reverse a number.



package demo;
import java.util.Scanner;
public class ReverseNumber
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  int num, r, reverse = 0;
  System.out.print("Enter any number: ");
  num = sc.nextInt();
  while (num != 0) 
  {
   r = num % 10;
   reverse = reverse * 10 + r;
   num = num / 10;
  }
  System.out.println("Reversed of number: " + reverse);
 }
}

Output:
Enter any number: 123789
Reversed of number: 987321
BUILD SUCCESSFUL (total time: 4 seconds)


No comments:

Post a Comment