Loops Questions-Solutions-1
Loops Questions-Solutions-1
Q 1.2 : Sum of first N natural numbers Write a program that takes an integer N as input and
calculates the sum of the first N natural numbers. (e.g., if N = 5, the sum will be 1+2+3+4+5 =
15)
Q2.1 : Print all even numbers between 1 and N
Write a program that takes a number N as input and print all even numbers between 1 and
N using a loop.
Q9.2 : Write a Java program to check whether the given number is prime or not. (Eg. For 7
it's true , for 10 it's false)
But in this question , everyone has to write an optimised code. A one which takes less time
to run and make the process faster. Ultimately you have to make it run faster.
A hint : Use the maths property like 2x6 = 6x2
Q9.1 : Check if the Sum of the Digits of a Number is Equal to the Product of its Digits
Write a program that takes an integer as input and checks if the sum of its digits is equal to the
product of its digits. (e.g., for 123, the sum is 1 + 2 + 3 = 6 and the product is 1 * 2 * 3 = 6, so the
output should be true.)
Q10.1 : Sum of Divisors of a Number
Write a program that takes an integer N and calculates the sum of all its divisors, including 1
and N. (e.g., for 12, the divisors are 1, 2, 3, 4, 6, 12, and their sum is 28)
https://youtu.be/yaRq7UamSR0
https://youtu.be/yaRq7UamSR0
https://youtu.be/yaRq7UamSR0 https://youtu.be/yaRq7UamSR0
Q14.1 : Right-Angled Triangle of Stars
Write a program to print a right-angled triangle of * symbols with N rows.
If N = 4
*
**
***
****
**
*
Q16.1 : Write the code for making the pattern using stars
If N = 5
*
* * *
*****
***
*
Q16.2 : Write the code for making the pattern using stars
If N = 7
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
Q17.1: Write the code for making the pattern
If N = 5
#
##
##
# #
#####
( Hollow
right
angled
triangle)
#
#
Q19.1: Write the code for making the pattern
If N = 5
#
##
# #
##
#
# #
# #
# #
#####
Q21.2: Perfect Number Checker: Write a program that checks if a given number N is a perfect number. A
perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). For
example, 28 is a perfect number because its divisors 1 + 2 + 4 + 7 + 14 = 28.
Q22.1: Write the code for making the pattern
If N = 5
1
11
101
1001
11111
*. *
**. **
***. ***
*******
*******
*******
*******
*
*. *
*. *
*. * * *
*. *
*. *
*. *
Q25.1 : Write the code for making the pattern
If N = 5
1
24
135
2468
13579