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
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
Explanation
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
Explanation
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
Explanation
Suggested for you
Do-while loop in Java language
Similar post
Reversed string in Java language
Reversed string in C++ language
C program to reverse a number using loops
C++ program to reverse a number using loops
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…