While loop

While Loop
While Loop

A while loop is an alternative loop structure that’s based on meeting a certain condition , rather than iterating a set number of times. the syntax of while loop is:

while(condition)
{
-
statement;
-
}

the operation is slightly different than the for loop , but the same operation can be implemented by for, while or do-while loops. the operation of the while loop is such that, first the condition is checked.if the condition is true, then the statement is executed the condition is again checked and this keeps on repeating until the condition is false.

1.reverse the number using while loop

import java.util.*;
import java.util.Scanner;
class NUM
{
public static void main(String args[])
{
int num=0,temp;
int reverse=0;
System.out.println("Enter the number and click enter:");
//this statement will capture the user input after enter clicked
Scanner in=new Scanner(System.in);
//entered input would be stored in number num
temp=num=in.nextInt();
//While Loop:find out the reverse number
while(num!=0)
{
reverse=reverse*10;
reverse=reverse+num%10;
num=num/10;
}
System.out.println("Reverse of input number"+temp+"is:"+reverse);
} 
}
OUTPUT:
Enter the number and click enter:
57689
Reverse of input number 57689 is:98675
Translate »