Reverse Number

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.
  1.          Reverse Number without user interaction
  2.          Reverse Number Using for loop
  3.          Reverse Number Using While loop
  4.          Reverse Number Using method
  5.          Reverse Number Using do-while loop

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.

import java.util.Scanner;
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

The program will prompt user to input the number and then it will reverse the same number using method.

import java.util.Scanner;

public class ReverseUsingMethod 
{
void reverse()
{
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);
}
public static void main(String[] args) 
{
ReverseUsingMethod obj = new ReverseUsingMethod();
obj.reverse();
}
}
Output:
Enter a Number: 
159874632
 Given Number:-159874632
 After Reverse :-236478951

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32