- It's body will execute until the given condition is true.

Here in the below program we have to print number from 1 to 9 so here we will take a single variable to make this program. Here we are taking variable x and we are initializing it with 1 and we will apply condition which is x < 10 that and we are incrementing x by one so output will be 1 2 3 4 5 6 7 8 9.

We can also write condition x<=9 in place of x < 10 and there will be no change in output.

class Easy { public static void main(String[] args) { int x=1; while(x<10) { System.out.println(x); x++; } } } /* ### Output ### 1 2 3 4 5 6 7 8 9 */

In the above example the body of while will execute again and again as long as variable

**x**is less than 10.To make this program using while loop we will use three variable one for storing number second for storing the multiplication result and third one for running loop.

After taking user input we will execute loop from 1 to number and we will multiply all the number from 1 to number and will store it to a variable and after the loop we will print the result which will be the factorial of of given number.

//factorial of 5=1x2x3x4x5 //factorial of 6=1x2x3x4x5x6 //factorial of N=1x2x3x....xN import java.util.Scanner; class Easy { public static void main(String[] args) { Scanner in=new Scanner(System.in); int no,f=1,i=1; System.out.println("Enter any number"); no=in.nextInt(); while(i<=no) { f=f*i; i++; } System.out.println("The factorial of "+no+" is "+f); } } /* ### Output ### Enter any number 6 The factorial of 6 is 720 */

Here in the above example the given number is 6 so loop will execute from 1 to 6 and the multiplication of 1 2 3 4 5 6 will store in variable f and at last we will print the result.

Request:-If you found this post helpful then let me know by your comment and share it with your friend.

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.

If you have not subscribed my website then please subscribe my website.

Try to learn something new and teach something new to other.

Thanks.ðŸ˜Š

## 0 Comments