Write a Java Program to find Reverse Number using While Loop

By | 20.01.2017

Write a Java Program to find Reverse Number using While Loop


Reverse number means reverse the position of all digits of any number.

For example : Reverse of 435 is 534.

For this program you need modulus operator concept and while loop, while loop is used for check condition and modulus used for find the remainder.

SOURCE CODE ::

 

import java.util.Scanner;

public class ReverseNumber {

public static void main(String[] args) {
 
 int n,i,reverse=0,k;
 
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter the no. which u want to reverse : ");
 n=sc.nextInt();
 k=n;
 while(n>0)
 {
 i=n%10;
 reverse=reverse*10+i;
 n=n/10;
 }
 System.out.println("Reverse of " + k + " is : "+ reverse);
 
 }
 
}

OUTPUT ::

Enter the no. which u want to reverse : 123456

Reverse of 123456 is : 654321
4.8 4 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sayani
while(n>0)

can I get an explanation of the above line

anonymous

it would be while(n!=0)