- On
- By
- 0 Comment
- Categories: do-while, for loop, function/method, Loop, While loop
How to find reverse number using method in Java
How to find reverse number using method
In this article, we will discuss the concept of the How to find reverse number of given number using method
In this post, we are going to learn how to find reverse of the given number in Java programming language
Code to reverse a number using the loop
Reverse a number using method in Java using for loop
The program allows the user to enter a number and then it displays the reverse number of the given number using for loop in Java language
Program 1
import java.util.Scanner; class Reversed_Num_Method{//class declaration static int reversed_Num=0;//global variable public static void main(String args[]){ int number;//local variable 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 number=scan.nextInt(); System.out.print("Reversed number of given number is: "+find_Reserve(number)); //call the method and display result } public static int find_Reserve(int number){//user-defined method for( ; number!=0;){ reversed_Num=reversed_Num*10; reversed_Num=reversed_Num+number%10; number=number/10; } return reversed_Num;//return the result to main method } }
When the above code is executed, it produces the following result
Reverse a number using method in Java using while loop
The program allows the user to enter a number and then it displays the reverse number of the given number using while loop in Java language
Program 2
import java.util.Scanner; class Reversed_Num_Methodwhile{//class declaration static int reversed_Num=0;//global variable public static void main(String args[]){ int number;//local variable 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 number=scan.nextInt(); System.out.print("Reversed number of given number is: "+find_Reserve(number)); //call the method and display result } public static int find_Reserve(int number){//user-defined method int rem; while(number>0){ rem=number%10; reversed_Num=reversed_Num*10+rem; number=number/10; } return reversed_Num;//return the result to main method } }
When the above code is executed, it produces the following result
Reverse a number using method in Java using do-while loop
The program allows the user to enter a number and then it displays the reverse number of the given number using do-while loop in Java language
Program 3
import java.util.Scanner; class Reversed_Num_Methoddowhile{//class declaration static int reversed_Num=0;//global variable public static void main(String args[]){ int number;//local variable 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 number=scan.nextInt(); System.out.print("Reversed number of given number is: "+find_Reserve(number)); //call the method and display result } public static int find_Reserve(int number){//user-defined method int rem; do{ rem=number%10; reversed_Num=reversed_Num*10+rem; number=number/10; }while(number>0); return reversed_Num;//return the result to main method } }
When the above code is executed, it produces the following result
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