0% found this document useful (0 votes)
15 views25 pages

Loops Questions-Solutions-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views25 pages

Loops Questions-Solutions-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Q 1.

1 : Print numbers from 1 to 10 Write a program to print the numbers from 1 to


10 using a loop

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.

Q2.2 : Multiplication table


Write a program that takes a number N as input and prints the multiplication table of
N up to 10. (e.g., for N = 5, the output should be 5×1=5, 5×2=10, ..., 5×10=50)
Q 3.1 : Factorial of a Number
Write a program that takes a positive integer N as input and calculates the factorial of N.
(e.g., if N = 5, the factorial is 5! = 5 × 4 × 3 × 2 × 1 = 120)

Q 3.2 : Find the Largest Digit in any given Number


Write a program that takes a positive integer as input and finds the largest digit in
that number. (e.g., for input 5483, the largest digit is 8)
Q4.1 : Print All Divisors of a Number
Write a program that takes a positive integer N as input and prints all its divisors. (e.g., for
N = 12, the output should be 1, 2, 3, 4, 6, 12)

Q4.2 : Reverse a number


Write a program that takes a number as input and prints the number in reverse. (e.g.,
if the input is 1234, the output should be 4321).
Q5.1 : Armstrong Number Check
Write a program that takes an integer N as input and checks if it is an Armstrong number.
An Armstrong number for a 3-digit number is a number equal to the sum of the cubes of its
digits. (e.g., 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153)

Q5.2 : Perfect Number Check


Write a program that takes an integer N as input and checks if it is a perfect number. A
perfect number is a positive integer equal to the sum of its proper divisors (excluding
itself). (e.g., 6 is a perfect number because 1 + 2 + 3 = 6)
Q6.1 : Sum of Factorials from 1 to N
Write a program that takes a positive integer N as input and calculates the sum of factorials
from 1 to N. (e.g., if N = 3, calculate 1! + 2! + 3! = 9)

Q6.2 : Check if a Number is a Strong Number


A strong number is a number whose sum of the factorial of digits is equal to the number
itself. Write a program to check if a given number is a strong number. (e.g., 145 is a strong
number because 1! + 4! + 5! = 145)
Q7.1 : Find Numbers with Exactly 3 Divisors till N
Write a program that prints all numbers from 1 to N that have exactly three divisors. (e.g.,
numbers like 4 have exactly 3 divisors: 1, 2, 4)

Q7.2 : Sum of Digits Raised to Consecutive Powers


Write a program that takes an integer as input and calculates the sum of each digit raised
to consecutive powers starting from 1. (e.g., for 234, the result would be 2^1 + 3^2 + 4^3 =
2 + 9 + 64 = 75)
Q8.1 : Count Number of Times a Digit Appears in All Numbers from 1 to N
Write a program that takes a digit D and a number N as inputs and counts how many times D
appears in all numbers from 1 to N. (e.g., if D = 2 and N = 25, the output should be 9 because 2
appears in 2, 12, 20, 21, 22, 23, 24, 25.)

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)

Q11.1 : Multiply Each Digit by Position


Write a program that takes an integer as input and multiplies each digit by its position (1-
indexed), then calculates the sum of these values. (e.g., for 234, calculate 2*1 + 3*2 + 4*3
= 2 + 6 + 12 = 20)
Q11.2 : Sum of Cubes of Digits
Write a program that takes an integer as input and calculates the sum of the cubes of all its
digits. (e.g., for 248, calculate 2^3 + 4^3 + 8^3 = 8 + 64 + 512 = 584)

Q12.1 : Sum of Prime Digits in a Number


Write a program that takes an integer as input and calculates the sum of its prime digits.
(e.g., for 2395, the prime digits are 2, 3, and 5, so the result is 10)
Q12.2 : Palindrome Number Check
A palindrome number is an integer that reads the same forward and backward. Write a program
to check if a given integer is a palindrome without converting it to a string. (e.g., 121 is a
palindrome, but 123 is not).

Q13.1 : GCD of Two Numbers


The Greatest Common Divisor (GCD) of two integers is the largest integer that divides
both numbers without leaving a remainder. Write a program to find the GCD of two given
integers. (e.g., for 8 and 12, the GCD is 4).

Q13.2 : LCM of Two Numbers


The Least Common Multiple (LCM) of two integers is the smallest integer that is a multiple
of both numbers. Write a program to find the LCM of two given integers. (e.g., for 8 and 12,
the LCM is 24).

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
*
**
***
****

Q14.2 : Inverted Right-Angled Triangle of Stars


Print an inverted right-angled triangle with N rows.
If N = 4
****
***
**
*
Q15.1 : Left-Aligned Right-Angled Triangle of Stars Write a program to
print a right-angled triangle of * symbols with N rows. If N = 4 * ** *** ****

Q15.2 : Inverted Right-Aligned Triangle of Stars


Print an inverted right-angled triangle 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)

Q17.2 : Write the code for making the pattern


If N = 5
#
#
#
#
#
Q18.1 : Write the code for making the pattern
If N = 5
# #
##
#
##
# #

Q18.2 : Write the code for making the pattern


If N = 5
#
#
#

#
#
Q19.1: Write the code for making the pattern
If N = 5
#
##
# #
##
#

Q19.2 : Write the code for making the pattern


If N = 5
1
23
456
78910
1112131415
Q20.1 : Write the code for making the pattern
If N = 5 #####

# #
# #
# #
#####

Q20.2 : Write the code for making the pattern


If N = 4
0
11
235
8 13 21 34 (Tip:- The numbers given are in Fibonacci series.)
Q21.1 : Sum of Digits Until Single Digit: Write a program that continuously sums the digits of a given number N until
the result is a single-digit number. For example, if N = 9875, the process would be 9 + 8 + 7 + 5 = 29, then 2 + 9 = 11, and
finally 1 + 1 = 2.

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

Q22.2 : Write the code for making the pattern


1
21
321
4321
54321
Q23.1 : Write the code for making the pattern
If N = 5
1
22
333
2222
11111

Q23.2 : Write the code for making the pattern


*****
** **
* * *
** **
*****
Q24.1 : Write the code for making the pattern
If N = 7

*. *
**. **
***. ***
*******
*******
*******
*******

Q24.2 : Write the code for making the pattern


If N = 7

*
*. *
*. *
*. * * *
*. *
*. *
*. *
Q25.1 : Write the code for making the pattern
If N = 5
1
24
135
2468
13579

Q25.2: Write the code for making the pattern


If N = 5
12345
2345
345
45
5
Q26.1 : Write the code for making the pattern
If N = 4
* *
** **
** * * * *
********

Q26.2 : Write the code for making the pattern


If N = 5
1 2
12 34
123 456
1234 5678
123456789

You might also like