V.Vidhya Logo

V.Vidhya

Loops


Q1.

Print first 25 whole numbers one-by-one, each in a new line.

Write this program using while loop.


Q2.

Print all even numbers between 4 and 60, in a single line separated by a space in between.

Write this program using do-while loop.


Q3.

Print all odd numbers between 1 and 50 in reverse order.

Write this program using for loop.


Q4.

Write a program to print all the numbers that are not divisible by 4, between 1 and 50.

Write this program using all types of loops i.e. while, do-while, for one by one.


Q5.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q6.

Write a program that -


Q7.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q8.

A local weather station logs the rainfall (in mm) for each month of the year. Write a program that:

Expected Output
Enter rainfall for Month 1 (in mm): 100
Enter rainfall for Month 2 (in mm): 80
Enter rainfall for Month 3 (in mm): 90
Enter rainfall for Month 4 (in mm): 75
Enter rainfall for Month 5 (in mm): 60
Enter rainfall for Month 6 (in mm): 55
Enter rainfall for Month 7 (in mm): 40
Enter rainfall for Month 8 (in mm): 45
Enter rainfall for Month 9 (in mm): 70
Enter rainfall for Month 10 (in mm): 85
Enter rainfall for Month 11 (in mm): 95
Enter rainfall for Month 12 (in mm): 110

Total rainfall for the year: 905 mm
Average monthly rainfall: 75.42 mm

Q9.

Fizz Buzz is a simple math game used to teach children about divisibility. It is also sometimes used as an interview question to assess basic programming skills. The rules of the game are simple: Starting at 1, and counting upward, replace any number divisible only by three with the word “fizz”, any number only divisible by five with the word “buzz”, and any number divisible by both 3 and 5 with the word “fizzbuzz”.)

Take 2 numbers as user input and then print all the numbers starting from first given number, incrementing one by one, to second given number at last, but for any number divisible only by 3 print the word “fizz”, any number only divisible by 5 print the word “buzz” and any number divisible by both 3 and 5 print the word “fizzbuzz”.

Expected Output
Enter first number: 9   // user input
Enter Second number: 28   // user input

fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
22
.
.
.
28

Q10.

What will be the output of the following programs?

You have to predict the output result without running/executing the code.


Q11.

You need a quick black-and-white checkerboard background for an old-school game screen. For it, write a program that asks the user for the board size N (an even number) and prints an NxN checkerboard of X and O characters, alternating each cell and each row.

Expected Output:

Enter board size (even number): 6   // user input

X O X O X O
O X O X O X
X O X O X O
O X O X O X
X O X O X O
O X O X O X

Q12.

At a grocery checkout, the cashier enters each item’s price one by one. Write a program that:

Expected Output
Enter price of item (or 0 to exit): 120   // user input
Enter price of item (or 0 to exit): 85   // user input
Enter price of item (or 0 to exit): 40   // user input
Enter price of item (or 0 to exit): 55   // user input
Enter price of item (or 0 to exit): 0   // user input

Total Bill: rs.300

Q13.

A fitness app asks the user each day how many steps they walked, for a 7-day challenge. Write a program that:

Expected Output
Day 1 steps: 12000
Day 2 steps: 8000
Day 3 steps: 10000
Day 4 steps: 15000
Day 5 steps: 5000
Day 6 steps: 10000
Day 7 steps: 11000

You met your 10,000 steps goal on 5 out of 7 days.

Q14. (Adv.)

A popular music festival is happening in town, and the organizer has invited N different bands to perform on the main stage. Since the event has only one stage, the performances will happen one after the other — no two bands can perform at the same time.

Now, the organizer wants to explore all the possible ways these bands could be scheduled throughout the day. This is important because the sequence in which the bands perform can impact the audience experience, energy levels, and crowd engagement.

For example, should the high-energy band go first, or last? Should the acoustic group follow the rock band, or should they go earlier? To make these decisions wisely, the organizer first wants to know: “In how many different ways can I arrange the performance order for today’s bands?”

Write a program that:

Expected Output
 // case 1
Enter the number of bands:: 4   // user input

Total possible lineups: 24

// case 2
Enter the number of bands:: 6   // user input

Total possible lineups: 720

Q15.

A new café wants a big, eye-catching sign around its name. To help them, write a program that asks the user for the width and height of the sign, then draw a rectangular border of * characters of that size. The inside should be blank.

Expected Output:

Enter width: 10   // user input
Enter height: 5   // user input

* * * * * * * * * *
*                 *
*                 *
*                 *
* * * * * * * * * *

Q16.

At an event entrance, the staff record the ages of attendees. The survey wants to know how many are minors (< 18) and how many are adults (≥ 18). Write a program that:

Expected Output
How many attendees to survey: 5

Enter age of attendee 1: 16
Enter age of attendee 2: 20
Enter age of attendee 3: 18
Enter age of attendee 4: 15
Enter age of attendee 5: 30

Survey Results:
Minors (under 18): 3
Adults (18 and over): 2

Q17.

A coach wants a quick strike-rate report for every batsman in a small squad, each facing a fixed number of balls. To solve this problem, write a program that:

Expected Output
Enter number of batsmen in the squad: 3
Enter number of balls faced by each batsman: 6

--- Squad Strike-Rate Report ---

Runs scored by batsman-1 on ball 1: 1
Runs scored by batsman-1 on ball 2: 4
Runs scored by batsman-1 on ball 3: 0
Runs scored by batsman-1 on ball 4: 2
Runs scored by batsman-1 on ball 5: 3
Runs scored by batsman-1 on ball 6: 0

Batsman-1: Total Runs: 10, Strike Rate: 166.67%

Runs scored by batsman-2 on ball 1: 0
Runs scored by batsman-2 on ball 2: 2
Runs scored by batsman-2 on ball 3: 6
Runs scored by batsman-2 on ball 4: 4
Runs scored by batsman-2 on ball 5: 2
Runs scored by batsman-2 on ball 6: 4

Batsman-2: Total Runs: 18, Strike Rate: 300.0%

Runs scored by batsman-3 on ball 1: 4
Runs scored by batsman-3 on ball 2: 1
Runs scored by batsman-3 on ball 3: 0
Runs scored by batsman-3 on ball 4: 0
Runs scored by batsman-3 on ball 5: 2
Runs scored by batsman-3 on ball 6: 1

Batsman-3: Total Runs: 8, Strike Rate: 133.33%

Q18. (Adv.)

A fruit wholesaler receives apple crates every X days and orange crates every Y days. Each apple delivery brings A crates and each orange delivery brings B crates. The wholesaler stores all incoming crates and wants to pack mixed baskets so that every basket has the same number of apple and orange crates, using up the entire stock on one of the pack-days.

To solve the fruit wholesaler’s problem, write a program that asks the user to enter:

Then calculate and display:

Expected Output
// case 1
Enter apple delivery interval (days): 12  
Enter apple crates per delivery: 36  
Enter orange delivery interval (days): 15  
Enter orange crates per delivery: 45  

Maximum crates per basket: 9  
Next matching delivery in: 60 days


// case 2
Enter apple delivery interval (days): 9  
Enter apple crates per delivery: 25
Enter orange delivery interval (days): 6
Enter orange crates per delivery: 10

Maximum crates per basket: 5
Next matching delivery in: 18 days

Prev Post
Conditional Statements
Next Post
Strings