java

Java Program to Reverse a Number

In this tutorial, we are going to see how to write a Java program to reverse a number. For example, if the number entered by the user is 231, the output will be 132.
 

Java Program to Reverse a Number :
import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  {
    int nbr, r = 0;

    System.out.print("Enter a number to reverse : ");
    Scanner sc = new Scanner(System.in);
    nbr = sc.nextInt();

    while(nbr != 0)
    {
      r = r * 10;
      r = r + nbr%10;
      nbr = nbr/10;
    }

    System.out.println("The reverse of the number is " + r);
  }
}

Output:

Enter a number to reverse : 231
The reverse of the number is 132
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *