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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…