Exercises 4 - Loop Problems

Print numbers from 0 to N


Print numbers from 0 to N in reverse order


Print all odd numbers between 0 to N


Print all even numbers between 0 to N


Print tables of 2 like below

2  *  1   =  2
2  *  2   =  4
2  *  3   =  6
2  *  4   =  8
2  *  5   =  10
2  *  6   =  12
2  *  7   =  14
2  *  8   =  16
2  *  9   =  18
2  *  10  =  20

Print tables of N same as above.

Example: If N = 2,


Find sum of all numbers between 0 to N

Input: N = 5

Output:

Sum = 15

Explanation:

1 + 2 + 3 + 4 + 5 = 15


Find sum of all even numbers between 0 to N

Input: N = 5

Output:

Sum of Even Numbers = 6

Explanation:

Even numbers between 0 and 5 are 2 and 4

So 2 + 4 = 6


Find sum of all odd numbers between 0 to N

Input: N = 5

Output:

Sum of odd numbers = 9

Explanation:

Odd numbers between 0 and 5 are 1,3 and 5.

So 1 + 3 + 5 = 9


Fizz Buzz Problem

Input: N = 15

Output:

Print 1 to 15 like below.

If number is divisible by 3, print "Fizz"

If number is divisible by 5, print "Buzz"

If number is divisible by both 3 and 5, print "FizzBuzz"


Find the factorial of N

Factorial:

Factorial of 5 ( 5! ) = 5 * 4 * 3 * 2 * 1 = 120

Input:

N = 5

Output:

Factorial of 5 = 120


Find whether the number is prime number or not

What is prime number ?

Number is divisible by 1 and number itself

Input:

N = 13

Output:

13 is Prime number

Explanation:

13 is divisible by 1 and 13. It will not be divisible by any other number


Find how many magic numbers between 1 to N

Magic number is a positive number that divisible by both 3 and 5.

Input:

N = 30

Output:

Total 2 magic numbers found between 1 to 30.

Explanation:

Only 15, 30 are the divisible by both 3 and 5.


Last updated