Java program to reverse a number using loops

Java program to reverse a number using loops

In this article, we will discuss the concept of the Java program to reverse a number using loops.

In this post, we are going to learn how to find reverse number of the given number in Java programming language

Reversed number

Java code to reverse a number using for loop

Program 1

The program allows  the user to enter a number and it displays the reversed pattern of the given number using for loop in Java language

when you are entered 34567,
The output will be displayed as 76543

 

import java.util.Scanner;
class Reversed_Num{
public static void main(String args[]){
int num,reversed_Num=0;
Scanner scan=new Scanner(System.in);
//This statements takes input from theuser
System.out.print("Enter the number for find reverse: ");
//get input and it is stored num variable
num=scan.nextInt();
for( ; num!=0;){
reversed_Num=reversed_Num*10;
reversed_Num=reversed_Num+num%10;
num=num/10;
}
System.out.print("Reversed number of given number is: "+reversed_Num);
}
}

When the above program is executed, it produces the following result

Program 1

Explanation

  • Declare and initialize two variables as follows  int num,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num
  • The for loop is used to create the reversed number of the given number
  • The for loop is functioning until ‘ num‘ is not equal to zero
  • Finally, the output is displayed as the reversed number

Java code to reverse a number using while loop

Program 1

The program allows  the user to enter a number and it displays the reverse pattern of the given number using while loop in Java language

when you are entered 12345,
The output will be displayed as 54321 using the program

 

import java.util.Scanner;
class Reversed_NumWhile{
public static void main(String args[]){
int num,rem,reversed_Num=0;
Scanner scan=new Scanner(System.in);
//This statements takes input from theuser
System.out.print("Enter the number for find reverse: ");
//get input and it is stored num variable
num=scan.nextInt();
while(num>0){
    rem=num%10;
    reversed_Num=reversed_Num*10+rem;
    num=num/10;
}
System.out.print("You entered : "+num);
System.out.print("Reverse of the number is: "+reversed_Num);
}
}

When the above program is executed, it produces the following result

Program 2

Explanation

  • Declare and initialize three variables as follows int num,rem,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num’
  • The while loop is used to create the reversed number of the given number
  • The while loop is functioning until ‘ num‘ is greater than zero
  • Finally, the output is displayed as the reversed number

Java code to reverse a number using do-while loop

Program 3

The program allows  the user to enter a number and it displays the reverse pattern of the given number using do-while loop in Java language

when you are entered 56789,
The output will be displayed as 98765 using the program

 

import java.util.Scanner;
class Reversed_NumDoWhile{
public static void main(String args[]){
int num,rem,reversed_Num=0;
Scanner scan=new Scanner(System.in);
//This statements takes input from theuser
System.out.print("Enter the number for find reverse: ");
//get input and it is stored num variable
num=scan.nextInt();
do{
    rem=num%10;
    reversed_Num=reversed_Num*10+rem;
    num=num/10;
}while(num>0);
System.out.print("You entered : "+num);
System.out.print("Reverse of the number is: "+reversed_Num);
}
}

When the above program is executed, it produces the following result

Program 3

Explanation

  • Declare and initialize three variables as follows int num,rem,reversed_Num=0;
  • The user is asked to enter a number and it is stored in the integer variable of ‘num’
  • The do-while loop is used to create the reversed number of the given number
  • The do-while loop is functioning until ‘ num‘ is greater than zero
  • Finally, the output is displayed as the reversed number

 

Suggested for you

For loop in Java language

While loop in Java language

Do-while loop in Java language

 

Similar post

Reversed string in Java language

Reversed string in C language

Reversed string in C++ language

C program to reverse a number using loops

C++ program to reverse a number using loops

 

 

 

Java code to reverse a string using loops
C++ program to reverse a number using loops
Java languageJava programs