Loops Questions For Absolute Beginners
Loops Questions For Absolute Beginners
(Part 2 - loops)
INDEX
INDEX 1
Return to index
3
Print Range from 1 to n
Given an integer n, print all integers in range 1 to n.
Input Format
Output Format
Sample Input
Sample Output
1
2
3
4
5
Explanation
Return to index
4
Print n to m
You will be given an input n and m of integer data-type.
5 6 7 8 9 10
10 9 8 7 6 5
Input Format
For each test case, you will be given the value of n and m as an integer-input.
Output Format
Sample Input
4
11
Sample Output
4 5 6 7 8 9 10 11
11 10 9 8 7 6 5 4
Return to index
5
Multiple Of n
Take an integer n and m as input, and print all the multiples of n till m.
Input Format
Output Format
Sample Input
7
98
Sample Output
0 7 14 21 28 35 42 49 56 63 70 77 84 91 98
Print Even
Given a integer n, print all even numbers from 0 till n (including, if even)
Input Format
Output Format
Sample Input
10
Sample Output
0
2
4
6
8
10
Return to index
6
Print 3 7 11 15
You will be given an integer input n, and you have to print the series 3, 7, 11, 15 till the integer is
just less than n, in n different lines.
Input Format
Output Format
Sample Input
51
Sample Output
3 7 11 15 19 23 27 31 35 39 43 47
Explanation
Here 51 although a part of the series is still not printed as we have to print the series just less than
51.
Input Format
For each test case, you will get integer input n and m.
Output Format
You have to print all the odd numbers in an integer format from n to m.
Sample Input
30
10
Sample Output
29 27 25 23 21 19 17 15 13 11
Return to index
7
Print n, n-3, n-6
You will be given an input n of integer data type.
Important points:
Input Format
Output Format
Sample Input 0
20
Sample Output 0
20
17
14
11
8
5
2
Return to index
8
Print n, n-k, n-2k, n-3k
"Print the series n, n-k, n-2k in separate lines until the printed integer is greater than or equal to
zero, using the integers n and k as input."
Input Format
For each test case, n will be given as an integer input in the first line,
Output Format
Sample Input 0
30
4
Sample Output 0
30
26
22
18
14
10
6
2
Return to index
9
print a to z
You have to print characters from a till z using for or while loop
Input Format
Output Format
Input Format
Output Format
Sample Output 0
aBcDeFgHiJkLmNoPqRsTuVwXyZ
Input Format
Output Format
Print as given in the problem statement, where each character should be printed in a new line.
Sample Output 0
aCeGiKmOqSuWy
Return to index
10
Print series AAA,bb,CCC,dd,EEE,ff till 26 lines
Write a program to print AAA,bb,CCC,dd,EEE,ff,........uptil 26 lines ,where each String printed in a
separate line.
Output Format
Sample Output 0
AAA
bb
CCC
dd
EEE
ff
GGG
hh
III
jj
KKK
ll
MMM
nn
OOO
pp
QQQ
rr
SSS
tt
UUU
vv
WWW
xx
YYY
zz
Return to index
11
Print even or odd from a list of integers
First take n as an integer input.
Then you will be given n integers as integer inputs and each time you have to print "even" if the
number is an even number and "odd" if the number is an odd number.
Input Format
For each test case, You will be given an integer n of int data-type in the first line, After this you will
be given n integers each of int data-type in separate lines.
Output Format
Sample Input
3
10
13
14
Sample Output
even
odd
even
Return to index
12
Print n/3
Can you write a program for Alice, a computer science student, that takes an integer input from
the user, divides it by 3 and prints the result, continuing until the result is greater than 0? Bob, a
computer science professor, has given her this problem to help her understand a concept related
to loops.
Note: Start printing from n, keep on updating n by dividing n by 3 each time, and print the updated
value of n each time.
Input Format
The first line of input contains a single integer T denoting the number of test cases. The description
of T test cases follows. For each test case, n will be given as an integer input.
Output Format
Sample Input
3
78
35
24
Sample Output
78 26 8 2
35 11 3 1
24 8 2
Return to index
13
Multiples of 3, 5 and Both 3 and 5
Can you help Maria, a math teacher, write a program that takes an integer n as input and returns a
list of all the multiples of 3, 5, and both 3 and 5 within a range of 1 to n for her lesson plan activity
for her students?
Input Format
The first line of input contains a single integer T denoting the number of test cases. The description
of T test cases follows. The first and the only line of each test case contains an integer n.
Output Format
Sample Input 0
2
10
15
Sample Output 0
3 5 6 9 10
3 5 6 9 10 12 15
Return to index
14
FizzBuzz problem
The rules of the FizzBuzz game are given below:
Input Format
Output Format
Sample Input
21
Sample Output
Explanation
Return to index
15
Running sum and average
You are given an integer n.
Your task is to write a program to print the running sum from 1 to n and its average.
Input Format
Output Format
Sample Input 0
Sample Output
15
3
Explanation
Return to index
16
Running Sum for loop
"Maria, a student struggling with understanding how to find the running sum of a series of
integers, is given a problem by her math teacher: Given a series of n integers as input, print the
sum after taking input of each integer one at a time."
With guidance from her math teacher, Maria is able to successfully understand and solve a
problem of finding the running sum of a series of integers. The problem gives her an example
where the series of integers is 3, 4, 5, 6 and the output should be 3, 7, 12, 18. Initially, Maria is a
little confused but with practice, she is able to understand the concept.
Input Format
The first line of input contains a single integer T denoting the number of test cases. The description
of T test cases follows
For each test case, You will get the value n as an integer input in the first line, and n integers as
integer input in different lines.
Output Format
You have to print the running sum, each time in a different line.You will be given a number n of
integer data-type.
Sample Input
2
5
12345
3
253
Sample Output
1 3 6 10 15
2 7 10
Explanation
In the first line we receive 5, meaning five integer inputs will be given as input.
Initially before taking any integer input the sum is zero.
When we take in the first integer input which is 3, the sum becomes 3.
When we take in the second integer input which is 2, the sum becomes 5.
After 3rd input, the sum becomes 7.
After 4th input, sum becomes 6,
After the 5th input, the sum becomes 10.
Return to index
17
Fibonacci number
You have given an integer n , you have to print first n numbers of the fibonacci series till n.
Input Format
First and only line of input contains an integer n denoting the number.
Output Format
Sample Input
10
Sample Output
0 1 1 2 3 5 8 13 21 34
Input Format
Output Format
Sample Input
10
Sample Output
0 1 3 8 21
Explanation 0
Return to index
18
Nth Fibonacci Number
Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula
Input Format
The first line of each test case contains a real number ‘N’.
Where ‘N’ represents the number for which we have to find its equivalent Fibonacci number.
Output Format
Sample Input 0
Sample Output 0
Explanation 0
Now the number is ‘6’ so we have to find the “6th” Fibonacci number
1, 1, 2, 3, 5, 8
Sample Input 1
Sample Output 1
Return to index
19
Print nth Tribonacci number
nth term Tn of The Tribonacci sequence is defined as follows:
T0(0th term) = 0, T1(1st term) = 1, T2(2nd term) = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Take n as an integer input, print the value of Tn(nth term) as an integer output.
Input Format
Output Format
0 0 1 1
2 1 7 24
10 149 11 247
20 66012
Return to index
20
Print from n to 3 using while loop
Print numbers from n to 3 using a while loop where n is taken as input from the user.
Input Format
Output Format
Sample Input
-9
Sample Output
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
Explanation
Return to index
21
Find Permutation
Given n and r, find the value of nPr. ( formula of npr=n!/(n-r)! )
Input Format
Output Format
Sample Input
5
2
Sample Output
20
Explanation
Take n = 5 and r = 2.
Return to index
22
Steps till n greater than 0
Can you help Jake, a data analyst, come up with a solution for a problem of simulating the
operation of a machine learning model by writing a program which takes an integer input n,
performs a series of steps until the value of n becomes 0, subtract 1 from n if n is even, subtract 3
from n if n is odd and keep track of the total number of steps that the program performs and print
this value at the end of the operation for his project?
Input Format
The first line of input contains a single integer T denoting the number of test cases. The description
of T test cases follows. For each test case, you will be given the value of n as an integer data-type.
Output Format
Sample Input
2
20
37
Sample Output
10
19
Return to index
23
Print all digits from end
Imagine Charlie is a high school student who is learning programming as a hobby. One day, he
comes across the following problem:
"Write a program that takes an integer input from the user. The program should print the digits of
the number starting from the end, going towards the first digit, where each digit should be printed
on a separate line. Can you write a solution for this problem?"
Input Format
The first line of input contains a single integer T denoting the number of test cases. The description
of T test cases follows. For each test case, print n as an integer input
Output Format
Print digits as an integer output as given in the problem statement, where each digit should be
printed in a separate line.
7654 4
5
6
7
87543 3
4
5
7
8
987651 1
5
6
7
8
9
Return to index
24
Number of Digits
Take an integer N as input.
Input Format
Output Format
Sample Input
523
Sample Output
Explanation
Return to index
25
Print total steps when n/2
Take an integer input n and then keep on dividing n by 2, till the time n is greater than equal to 1.
Input Format
Output Format
Sample Input 0
32
Sample Output 0
Sample Input 1
20
Sample Output 1
Sample Input 2
34
Sample Output 2
Sample Input 3
109
Return to index
26
Print steps and update maximum
Take n as input from the user. Then you will be given a list of n positive integers, each time you find
a new maximum value, you have to increment the steps by 1.
Input Format
Output Format
Sample Input 0
6
1
2
3
4
5
6
Sample Output 0
Sample Input 1
7
2
3
4
5
1
2
10
Sample Output 1
Return to index
27
Find GCD
Take two integer inputs x and y. Then print the gcd of these two numbers as an integer output.
Input Format
Output Format
Sample Input 0
100
35
Sample Output 0
Sample Input 1
300
20
Sample Output 1
20
Sample Input 2
500
37
Sample Output 2
Return to index
28
Calculate LCM
Take x and y as input. Write a function that takes in x and y as integer parameters. The function
should return the lcm of these two numbers. In the end print the final lcm.
Input Format
Output Format
Sample Input
15
20
Sample Output
60
Explanation
LCM of 15 and 20 is 60
Return to index
29
Reverse Digits
You are given a number n , Write a program to reverse digits of n.
Input Format
Output Format
Sample Input
1234
Sample Output
4321
Input Format
For each test case, n will be given as an integer input in the first line,
Output Format
Sample Input
4
1
2
3
6
Sample Output
1236
Return to index
30
Reverse n-digit number
Take a number n greater than or equal to zero as an integer input.
Then you will be given n digits as integer inputs and you have to form a number from it. Print the
number formed.
Then you have to reverse the digits of this number. And then print the final reversed number in the
next line.
Input Format
For each test case, n will be given as an integer input, then you will be given n digits as integer
inputs in each line.
Output Format
Print the number formed from the digits in the first line.
Sample Input 0
3
2
5
6
Sample Output 0
256
652
Sample Input 1
4
2
5
6
1
Sample Output 1
2561
1652
Return to index
31
Print Primes - 1
You are given two integer inputs x and y. Make a function that takes in x and y as parameters.
Then print all the prime numbers which lie between x and y (x and y both inclusive and y>x).
Input Format
Output Format
Sample Input 0
10
20
Sample Output 0
11 13 17 19
Explanation 0
Prime checker 2
Write a Java program to check whether a number is a Prime number or not.
Input Format
Output Format
Sample Input 0
Sample Output 0
Yes
Return to index
32
Print all factors of a number
Take a whole number n as an integer input and print all the factors of it such that each factor
should be printed in a separate line.
Input Format
Output Format
Print all the factors of the number where each factor should be printed in a separate line.
Sample Input 0
12
Sample Output 0
1
2
3
4
6
12
Sample Input 1
30
Sample Output 1
1
2
3
5
6
10
15
30
Return to index
33
Print all unique prime factors
Take a whole number n as an integer input and then print all the unique prime factors of n such
that each prime factor is printed in a separate line.
Input Format
Output Format
Print the prime factors as an integer value where each prime number should be printed in a
separate line.
45 3 100 2
5 5
240 2 350 2
3 5
5 7
Return to index
34
Check if an Armstrong number or not
Take n as an integer input.
Note: An armstrong number is a number which is equal to the sum of the cube of its digits.
Input Format
Output Format
Sample Input 0
782
Sample Output 0
false
Sample Input 1
153
Sample Output 1
true
Sample Input 2
370
Sample Output 2
true
Return to index
35
Print Armstrong in a range
Take x and y as integer inputs.
Print all the Armstrong numbers in separate line which lie in the range x to y (both x and y
inclusive)
Use the function isArmstrong() which checks if a number is an Armstrong number or not and
returns true or false accordingly.
Input Format
Output Format
Print the numbers as integer outputs where each number is printed in a separate line.
Sample Input 0
1
200
Sample Output 0
1
153
Return to index
36
Rotate 7-digit number to right by three
Take n as an integer input, you have to pick the last 3 digits of the number of and put them in the
starting.
Input Format
Constraints
Output Format
Sample Input 0
2345678
Sample Output 0
6782345
Sample Input 1
1236789
Sample Output 1
7891236
Return to index
37
Divide n by 2 3 5 and tell steps
Take a natural number n as an integer input, and variable steps of integer type as input. Then
perform the following operations on it.
1. If the number is divisible by 2, then keep on dividing the number n by 2, till the time the
number is divisible by 2 and also increment the variable steps by 2, each time you divide
the number by 2.
2. Also, check If the number is divisible by 3, then keep on dividing the number n by 3, till the
time the number is divisible by 3 and also increment the variable steps by 3, each time you
divide the number by 3.
3. Also, If the number is divisible by 5, then keep on dividing the number n by 5, till the time
the number is divisible by 5 and also increment the variable steps by 5, each time you
divide the number by 5.
In the end print the value of the variable steps in the first line and final value of number n in the
second line.
Input Format
Output Format
Print the final value of steps in the first line as an integer output,
and print the final remaining value of n in the second line as an integer output.
30 10 100 34
0 1 20 1
210 17 243 15
7 7 0 1
Return to index
38
Odd Even and Divisibility by 3
Take n as an integer input. After this you will be given n numbers as integer inputs and you have to
print each time if the number is Even or Odd. And Print “Divisible by 3” if the number is a multiple
of 3 and print “Not Divisible by 3” if the number is not a multiple of 3
Input Format
● N as an Integer Input
● N numbers as an Integer Input
Output Format
Sample Input 0
5
9
11
2
6
15
Sample Output 0
Odd Divisible by 3
Odd Not Divisible by 3
Even Not Divisible by 3
Even Divisible by 3
Odd Divisible by 3
Return to index
39
Even Odd 2
First Take an integer t as an input. Then you will be given t number test cases.
Then take n integer inputs, and for each one of these inputs, print "Even" if the given integer is an
even number and print "Odd" if the given number is an odd number. "
Make sure to write the complete output for each test case in a separate line.
Also, within the same line give a space between two words.
Input Format
From the next line onward for each test case, first you will get an integer input n.
Then for the same test case, you will get n integer numbers.
Output Format
Make sure to write the complete output for each test case in a separate line.
**Also, within the same line give a space between two words.
Sample Input 0
2
4
10 21 33 35
3
27 31 32
Sample Output 0
Even Odd Odd Odd
Odd Odd Even
Explanation 0
Here t is 2. So we will be having 2 test cases. Then in the first test case, we have n=4, so we have 4
numbers in total. Then we are given 4 numbers. 10 is even so we print Even. 21 is odd so we print
Odd. 33 is odd so we print Odd. 35 is odd so we print Odd.
Then for the second test case we have n=3, so we have 3 numbers in total. So we print Odd Odd
Even
Return to index
40
Sample Input 1
4
1
11
2
12 14
5
22 33 44 57 99
4
12 15 17 19
Sample Output 1
Odd
Even Even
Even Odd Even Odd Odd
Even Odd Odd Odd
Sample Input 2
2
2
12 13
3
11 98 97
Sample Output 2
Even Odd
Odd Even Odd
Sample Input 3
3
2
17 29
3
12 39 45
4
3 2 1 -480
Sample Output 3
Odd Odd
Even Odd Odd
Odd Even Odd Even
Return to index