Loop related problems (total 20 questions)
Difficulty
SL Problem statement
levels
1. Write a program (WAP) that will print following series upto Nth terms. *
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, …….
Sample input Sample output
2 1, 2
5 1, 2, 3, 4, 5
11 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
2. Write a program (WAP) that will print following series upto Nth terms. *
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 …….
Sample input Sample output
2 1, 3
5 1, 3, 5, 7, 9
11 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21
3. Write a program (WAP) that will print following series upto Nth terms. **
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, …….
Sample input Sample output
1 1
2 1, 0
3 1, 0, 1
4 1, 0, 1, 0
7 1, 0, 1, 0, 1, 0, 1
13 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
4. Write a program (WAP) that will take N numbers as inputs and compute their *
average.
(Restriction: Without using any array)
Sample input Sample output
3 AVG of 3 inputs: 20.166667
10 20
30.5
2 AVG of 2 inputs: 16.750000
22.4 11.1
5. Write a program (WAP) that will take two numbers X and Y as inputs. Then it *
will print the square of X and increment (if X<Y) or decrement (if X>Y) X by 1,
until X reaches Y. If and when X is equal to Y, the program prints “Reached!”
Sample input(X,Y) Sample output
10 5 100, 81, 64, 49, 36, Reached!
5 10 25, 36, 49, 64, 81, Reached!
10 10 Reached!
6. Write a program (WAP) for the described scenario: **
Player-1 picks a number X and Player-2 has to guess that number within N
tries. For each wrong guess by Player-2, the program prints “Wrong, N-1
Choice(s) Left!” If Player-2 at any time successfully guesses the number, the
program prints “Right, Player-2 wins!” and terminates right away. Otherwise
after the completion of N wrong tries, the program prints “Player-1 wins!” and
halts.
(Hint: Use break/continue)
Sample input Sample output
(X,N,n1, n2,..,nN)
5 Wrong, 2 Choice(s) Left!
3 Wrong, 1 Choice(s) Left!
12 8 5 Right, Player-2 wins!
100 Wrong, 4 Choice(s) Left!
5 Right, Player-2 wins!
50 100
20 Wrong, 2 Choice(s) Left!
3 Wrong, 1 Choice(s) Left!
12 8 5 Wrong, 0 Choice(s) Left!
Player-1 wins!
7. Write a program (WAP) that will run and show keyboard inputs until the user *
types an ’A’ at the keyboard.
Sample input Sample output
X Input 1: X
1 Input 2: 1
a Input 3: a
A
8. Write a program (WAP) that will reverse the digits of an input integer. **
Sample input Sample output
13579 97531
4321 1234
9. Write a program (WAP) that will find the grade of N students. For each *
student, it will take the marks of his/her attendance (on 5 marks), assignment
(on 10 marks), class test (on 15 marks), midterm (on 50 marks), term final (on
100 marks). Then based on the tables shown below, the program will output
his grade.
Attendance (A) 5%
Assignments (HW) 10%
Class Tests (CT) 15%
Midterm (MT) 30%
Final (TF) 40%
Marks Letter Marks Letter Marks Letter Grade
Grade Grade
90-100 A 70-73 C+ Less than F
55
86-89 A- 66-69 C
82-85 B+ 62-65 C-
78-81 B 58-61 D+
74-77 B- 55-57 D
Sample input (A,HW,CT,MT,TF) Sample output
2 Student 1 : A
5 10 15 44.5 92.5 Student 2 : F
0 7.5 5 20 55.5
10. Write a program (WAP) that will give the sum of first Nth terms for the **
following series.
1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, …….
Sample input Sample output
2 Result: -1
3 Result: 2
4 Result: -2
11. Write a program (WAP) that will calculate the result for the first Nth terms of **
the following series. [In that series sum, dot sign (.) means multiplication]
12.2 + 22.3 + 32.4 + 42.5 + …….
Sample input Sample output
2 Result: 14
3 Result: 50
4 Result: 130
7 Result: 924
12. Write a program (WAP) that will print Fibonacci series upto Nth terms. **
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …….
Sample input Sample output
1 1
2 1, 1
4 1, 1, 2, 3
7 1, 1, 2, 3, 5, 8, 13
13. Write a program (WAP) that will print the factorial (N!) of a given number N. **
Please see the sample input output.
Sample input Sample output
1 1! = 1 = 1
2 2! = 2 X 1 = 2
3 3! = 3 X 2 X 1 = 6
4 4! = 4 X 3 X 2 X 1 = 24
14. Write a program (WAP) that will find nCr where n >= r; n and r are integers. **
Sample input Sample output
5 2 10
10 3 120
7 7 1
6 1 6
15. Write a program (WAP) that will find xy (x to the power y) where x, y are *
positive integers.
Sample input(x,y) Sample output
5 2 25
2 0 1
6 1 6
0 5 0
16. WAP that will find the GCD (greatest common divisor) and LCM (least common **
multiple) of two positive integers.
Sample input Sample output
5 7 GCD: 1
LCM: 35
12 12 GCD: 12
LCM: 12
12 32 GCD: 4
LCM: 96
17. WAP that will determine whether a number is prime or not. **
Sample input Sample output
1 Not prime
2 Prime
11 Prime
39 Not prime
101 Prime
18. WAP that will determine whether an integer is palindrome number or not. **
Sample input Sample output
9 Yes
91 No
222 Yes
12321 Yes
110 No
19. WAP that will calculate following mathematical function for the input of x. Use ***
only the series to solve the problem.
Sample input Sample output
1 0.841
2 0.909
3 0.141
20. Write a program that takes an integer number n as input and find out the sum **
of the following series up to n terms.
1 + 12 + 123 + 1234 + …….
Sample input Sample output
1 1
2 13
3 136
4 1370