DEV Community

Sivakumar Mathiyalagan
Sivakumar Mathiyalagan

Posted on

While loop exercises

So far completed 9 exercises using while loop
1) Print the following 1 1 1 1 1
2) Print the following 1 2 3 4 5
3) Print the following 1 3 5 7 9
4)Print the following 3 6 9 12 15
5) Multiples of 3 and 5
6) Multiples of 3 or 5
7) Divisors of given number
8) Count of Divisors of given number
9) Prime Number

1st and 2nd exercise can be done just using while loop by declaring a variable with number of times we want to print and iterate the loop to print the numbers

3rd and 4th exercise need a condition statement (if.. else)to meet the exact condition in order to print the number for 3rd divisibles of 2 must be eliminated and for 4th only multiples of 3 must be printed

5th and 6th also needs a condition statement the only difference is that 5th exercise must be done with AND(&&) Operators and 6th must be done with OR(||) Operator

For 7th exercise a condition should be declared inside while loop (givenNumber % count ==0). givenNumber is the number to which we need to find the divisors and count starts from 1 till the givenNumber to find all its divisors

In 8th exercise we simply need to count the number of times the condition statement (if...else) executed because only if condition meets the execution will occur so by adding the number of time if statement executed we can print the count of divisors

Top comments (0)