JAVA PROGRAM TO REVERSE A GIVEN NUMBER

In this module we will learn about how to reverse a given number with multiple solutions

Table of Contents

[hide]
  • Java Program to Reverse a Given Number Using while Loop
  • Java Program to Reverse a Given Number Using while Loop(another way)
  • Java Program to Reverse a Given Number Using for Loop
  • Java Program to Reverse a Given Number Using recursion

Example 1 : Java Program to Reverse a Given Number Using while Loop

Algorithm

  • Take input from the user and store it in variable name num.

  • Pass this number to a method name reverse() which we can acces by the obj of ReverseDemo class.

  • A while loop is being used to iterate the text expression until n!=0

  • A variable name digit store the mod of n and 10 which is the last number of n.

  • Once we have stored the last number in the variable digit then we will divide the n with 10, so as to perform the same operation with the remaining numbers.

  • We keep on printing the last digit inside the while loop until n!=0 and while loop exits.

copyimage
// Java Program to reverse a given number
import java.util.Scanner;
class ReverseDemo{
    public void reverse(int n){
       while(n!=0) {
       int digit=n%10;
       n=n/10;
       System.out.println(digit);
    }
  }
}
public class Codebator{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       ReverseDemo obj=new ReverseDemo();
       obj.reverse(num);
	}
}

OUTPUT :

Enter any number34567 76543 

Example 2 : Java Program to Reverse a Given Number Using while Loop(another way)

copyimage
// Java Program to reverse a given number
import java.util.Scanner;
class ReverseDemo{
    public void reverse(int n){
       int rev=0;
       while(n!=0) {
       int digit=n%10;
       rev=rev*10+digit;
       n=n/10;
    }
    System.out.println(rev);
  }
}
public class Codebator{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       ReverseDemo obj=new ReverseDemo();
       obj.reverse(num);
   }
}

OUTPUT :

Enter any number12345 54321 

Example 3 : Java Program to Reverse a Given Number Using for loop

copyimage
// Java Program to reverse a given number using for loop
import java.util.Scanner;
class ReverseDemo{
    public void reverse(int n){
       for(;n!=0;n=n/10) {
       int digit=n%10;
       System.out.print(digit);
     }
  }
}
public class Codebator{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       ReverseDemo obj=new ReverseDemo();
       obj.reverse(num);
   }
}

OUTPUT :

Enter any number45678 87654 

Example 4 : Java Program to Reverse a Given Number Using Recursion

copyimage
// Java Program to reverse a given number using recursion
import java.util.Scanner;
class ReverseDemo{
    public void reverse(int n){
       if(n<10) {
       System.out.print(n);
       }
       else {
       System.out.print(n%10);
       reverse(n/10);
    }
  }
}
public class Codebator{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       ReverseDemo obj=new ReverseDemo();
       obj.reverse(num);
   }
}

OUTPUT :

Enter any number9876 6789 

Some useful links