Write a java program to Reversing a Number

Write a program to read a positive number as input and to get the reverse of the given number and return it as output. The return type is an integer value which is the reverse of the given number.

Input and Output Format

  • Input consists of a positive integer.
  • The output is an integer.

Refer sample output for formatting specifications

Sample Input 1:

543

Sample Output 1:

345

Sample Input 2:

1111

Sample Output 2:

1111

Program to Reverse a Number in Java

Following are the steps to reverse a number in Java:

  • Input a number from the user.
  • Declare and initialize variable rev and rem to 0. Store the input number into temporary variable d.
  • Repeat the following process till number becomes 0:
    • Modulo the number in the d variable by 10 and store the result in rem.
    • then, multiply the reverse number by 10 and add a rem variable value to it.
    • Then, divide the number by 10.
  • At last, print the number in the rev variable i.e a reverse number.
import java.util.Scanner;

public class Reverse {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int number= sc.nextInt();
int d=number,rev=0,rem=0;
while(d!=0)
{
rem=d%10;
rev=rev*10+rem;
d=d/10;
}
System.out.println(rev);

}
}

Output

Program to Reverse Number using for loop in Java

Following are the steps to reverse using for loop:

  • Input a number from the user.
  • Declare and initialize variable rev and rem to 0. 
  • Using for loop, initialize the number to d variable. And decrease the value of d by 10. 
  • Modulo the number in the d variable by 10 and store the result in rem.
  • Then, multiple the reverse number by 10 and add a rem variable value to it.
  • Then, divide the number by 10.
  • Repeat this process till the value in d is not equal to 0.
  • At last, print the value in variable rev.
package com.testcases;

import java.util.Scanner;

public class Reverse {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int rev = 0, rem = 0;
        for (int d = number;d != 0;d=d / 10) {
            rem = d % 10;
            rev = rev * 10 + rem;
        }
        System.out.println(rev);
    }
}

Output

Reverse Number Program using Recursion

Following are the steps to reverse using recursion:

  • Input a number from the user.
  • Pass the number to reverseNumber() method.
  • Inside the method, declare and initialize variable rev and rem to 0. Also, initialize variable d to the input number.
  • First,  the modulo of the number is calculated, and multiply the reverse number by 10 and add a rem variable value to it. 
  • Then, this method will call itself passing the quotient. This will call till the number is completed divided.
  • At last, print the value in rev variable.
package com.testcases;

import java.util.Scanner;

public class Reverse {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        reverseNumber(number);

    }

    private static void reverseNumber(int number) {
        int d = number;
        int rev = 0, rem = 0;
        if(d<10) {
            System.out.println(number);
        }else {
            rem = d % 10;
            rev = rev * 10 + rem;
            System.out.print(rev);
            reverseNumber(d / 10);
        }

    }
}

Output

Thus, in this way, we learn how to reverse numbers using for loop, while loop, and by using recursion.