Java Program To Reverse A
Number Using Recursion (OOP)
import java.util.Scanner;
public class ReverseNumberUsingClassA {
private static Scanner sc;
public static void main(String[] args) {
int Number, Reverse = 0;
sc = new Scanner(System.in);
System.out.print("Enter Any Number
You Want To Reverse: ");
Number = sc.nextInt();
ReverseNumberUsingClassB rn = new
ReverseNumberUsingClassB();
Reverse = rn.NumberReverse(Number);
System.out.format("\nReverse Of %d
= %d",Number, Reverse);
}}
ReverseNumberUsingClassB.java:
public class ReverseNumberUsingClassB {
int Reverse = 0, Reminder;
public int NumberReverse(int Number) {
if(Number > 0) {
Reminder = Number %10;
Reverse = Reverse * 10+ Reminder;
NumberReverse(Number /10);
}
return Reverse;
}}
import java.util.Scanner;
public class ReverseNumberUsingFor {
private static Scanner sc;
public static void main(String[] args) {
int Number, Reminder, Reverse;
sc = new Scanner(System.in);
System.out.print("Enter Any Number
You Want To Reverse: ");
Number = sc.nextInt();
int temp = Number; // not necessary in the code
for(Reverse = 0; Number > 0; Number =
Number/10) {
Reminder = Number %10;
Reverse = Reverse * 10 + Reminder;
}
System.out.format("\nReverse Of %d
= %d",temp, Reverse);
}}
Java Program To Reverse A
Number Using Method() Function
import java.util.Scanner;
public class ReverseNumberUsingMethods {
private static Scanner sc;
private static int Reverse = 0;
public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.print("Enter Any Number
You Want To Reverse: ");
Number = sc.nextInt();
Reverse = ReverseNumber(Number);
System.out.format("\nReverse Of %d
= %d",Number, Reverse);
}
public static int ReverseNumber(int
Number) {
int Reminder;
while(Number > 0) {
Reminder = Number %10;
Reverse = Reverse * 10 + Reminder;
Number = Number /10;
}
return Reverse;
}}
Java Program To Reverse A
Number Using Reverse Function
import java.util.Scanner;
public class ReverseNumberUsingSTring {
private static Scanner sc;
public static void main(String[] args) {
int number;
sc = new Scanner(System.in);
System.out.print("Enter Any Number
You Want To Reverse: ");
number = sc.nextInt();
int temp = number; // not necessary in the code
String str = Integer.toString(number);
String reverse = new
StringBuffer(str).reverse().toString();
System.out.format("\nReverse Of %d
= %s",temp, reverse);
}}
Java Program To Reverse A
Number Using While Loop
import java.util.Scanner;
public class ReverseNumberUsingWhile {
private static Scanner sc;
public static void main(String[] args) {
int Number, Reminder, Reverse = 0;
sc = new Scanner(System.in);
System.out.print("Enter Any Number
You Want To Reverse: ");
Number = sc.nextInt();
int temp = Number; // not necessary in the code
while(Number > 0) {
Reminder = Number %10;
Reverse = Reverse * 10 + Reminder;
Number = Number /10;
}
System.out.format("\nReverse Of %d
= %d",temp, Reverse);
}}