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

reversed the given number

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

find reverse number using for loop

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

find reverse number using while loop

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

find reverse number using for loop

 

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

Python program to reverse a number

Python program to reverse a given string

C# Full Pyramid star pattern program
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago