Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Write a Java program to print the entered number in reverse?

Reversing a number involves swapping the digit at the first position with the last digit and the second digit with the second last digit, and so on until the middle digit.

Here are the steps to reverse the number.

  • Take the number modulo you want to reverse by ten.
  • To determine the reverse number, multiply it by ten and add the modulo value.
  • Now divide the number by ten.
  • Until the number reaches zero, repeat the above steps.

Now let’s look at the below Java program that prints the number in reverse order.


import java.util.Scanner;
public class Reverse
{
    public static void main(String args[])
    {
        int x, y, k = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number you want to reverse:");
        x = s.nextInt();
        while(x > 0)
        {
            n = x % 10;
            k = k * 10 + y;
            x = x / 10;
        }
        System.out.println("Reverse number is "+k);
    }
}

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free