Java program to reverse a number using for, while, do-while, method and without user interaction.
There are different ways to reverse a number in Java.
Program 1: Reverse a number without user interaction
The program will prompt user to input the number and then it will reverse the same number without user interaction.
public class ReverseNumberWithoutUserInteraction
{
public static void
main(String[] args)
{
int number=159623487;
int reminder;
int reverse=0;
int temp=number;
while(number!=0)
{
reminder=number%10;
reverse=reverse*10+reminder;
number=number/10;
}
System.out.println("\n\n Given
Number:-"+temp);
System.out.println("\n After
Reverse :-" + reverse);
}
}
Output:
Enter a Number:
159874632
Given Number:-159874632
After Reverse :-236478951
Program 2: Reverse a number using for Loop
The program will prompt user to input the number and then it will reverse the same number using for loop.
import java.util.Scanner;
public class ReverseUsingFor
{
public static void
main(String[] args)
{
int reminder,reverse=0,temp;
Scanner scan = new
Scanner(System.in);
System.out.println("\n\nEnter a
Number: ");
int n = scan.nextInt();
temp=n;
for( ; n!=0 ; )
{
reminder=n%10;
reverse=reverse*10+reminder;
n=n/10;
}
System.out.println("\n Given
Number:-"+temp);
System.out.println("\n After
Reverse :-" + reverse);
}
}
Output:
Enter a Number:
159874632
Given Number:-159874632
After Reverse :-236478951
Program 3: Reverse a number using while Loop
The program will prompt user to input the number and then it will reverse the same number using while loop.
public class reverse
{
public static void main(String[] args)
{
int reminder,reverse=0,temp;
Scanner scan = new Scanner(System.in);
System.out.println("\n\nEnter a Number: ");
int n = scan.nextInt();
temp=n;
while(n>0)
{
reminder=n%10;
reverse=reverse*10+reminder;
n=n/10;
}
System.out.println("\n Given Number:-"+temp);
System.out.println("\n After Reverse :-" + reverse);
}
}
Output:
Enter a Number:
159874632
Given Number:-159874632
After Reverse :-236478951
Program 4: Reverse a number using do-while Loop
The program will prompt user to input the number and then it will reverse the same number using do-while loop.
import java.util.Scanner;
public class ReverseDoWhile
{
public static void
main(String[] args)
{
int
reminder,reverse=0,temp;
Scanner scan = new
Scanner(System.in);
System.out.println("\n\nEnter a Number: ");
int n =
scan.nextInt();
temp=n;
do
{
reminder=n%10;
reverse=reverse*10+reminder;
n=n/10;
}while(n!=0);
System.out.println("\n
Given Number:-"+temp);
System.out.println("\n
After Reverse :-" + reverse);
}
}
Output:
Enter a Number:
159874632
Given Number:-159874632
After Reverse :-236478951
public class ReverseDoWhile
{
public static void main(String[] args)
{
int reminder,reverse=0,temp;
Scanner scan = new Scanner(System.in);
System.out.println("\n\nEnter a Number: ");
int n = scan.nextInt();
temp=n;
do
{
reminder=n%10;
reverse=reverse*10+reminder;
n=n/10;
}while(n!=0);
System.out.println("\n Given Number:-"+temp);
System.out.println("\n After Reverse :-" + reverse);
}
}
Output:
Enter a Number:
159874632
Given Number:-159874632
After Reverse :-236478951
The program will prompt user to input the number and then it will reverse the same number using method.
No comments:
Post a Comment
If you have any doubts, please let me know