Lab 3
Lab 3
LAB - 03
While, Do – While, For and Nested Loops in C++
Question# 01
Learning Loop Patterns
Ahmad, a primary school teacher, wants to teach his students about loops through an interactive
program. He plans to create a program demonstrating three loop types: while, do-while, and for
loops. By showcasing patterns of stars and numbers, Ahmad aims to make learning programming
fundamentals enjoyable for his students.
Specifications:
1. While Loop: Print ten stars (*) using a while loop.
2. Do-While Loop: Display numbers from 0 to 9 using a do-while loop.
3. For Loop: Print numbers from 9 to 0 with a for loop.
4. Each loop structure generates its pattern independently, labeled clearly for easy
understanding by Ahmad's students.
[OUTPUT SCREEN]
******* ***
0123456789
9876543210
Question# 02
Learning Loop Pattern: Twinkling Triangle
Mr. Shahran is organizing an arts and crafts event for kids. He wants to decorate the venue with a
triangle made of twinkling stars.
Specifications:
Mr. Shahran needs your assistance to develop a program that will display stars in the shape of a
triangle. Each row of stars will increase by one, forming a triangular pattern. Create a C++
program to help Shahran bring his starry decoration to life.
[OUTPUT SCREEN]
*
**
***
****
*****
Question# 03
Learning Loop Pattern: Glowing and Shrinking Triangle
Miss Sophia, a preschool teacher, is planning a fun activity for her students. She wants to create a
glowing and shrinking triangle made of stars to decorate the classroom.
Specifications:
Sophia needs your help to design a program that will display stars in the shape of a glowing
triangle first and then start shrinking. The triangle should have five rows of stars, with the middle
row being the longest and the top and bottom rows having one less star. Can you create a C++
program to assist Sophia in creating this sparkling decoration for her classroom?
[OUTPUT SCREEN]
**
***
****
*****
****
***
**
Question# 04
Weekly Working Hours and Salary Calculator
Sarah is a manager at a small company and is responsible for tracking the working hours of her
employees. The company has a standard working hour policy of 7 hours per day. However, Sarah
finds it challenging to compute the total working hours per week for each employee, along with
their salary. Additionally, she wants to identify exceptional employees who consistently work
more than the standard hours and reward them with the title of "Good Employee".
Create a program that assists Sarah in calculating the total working hours, salary, and employee
performance based on their weekly working hours.
Specifications:
1. Prompt the user to enter the actual working hours of an employee for each day of the week.
2. Use a do-while loop to ensure that the entered working hours for each day are valid (greater
than or equal to 0 and less than or equal to 10).
3. Calculate the total working hours for the week and display it.
4. Calculate the salary for the week based on the fixed hourly rate (i.e., 5$ per Hour).
5. Determine the number of days the employee took leave (where the working hours are 0).
6. Identify the number of days the employee worked overtime (where the working hours
exceed 7 hours per day).
7. If the total working hours for the week greater than the standard working hours (7
hours/day X 5 days = 35 hours), and the Days Present between 5 and 7 then assign the title
"Good Employee" to the employee otherwise “Usual One!”.
[OUTPUT SCREEN]
Enter Hours Day – 1 = 9
Enter Hours Day – 2 = 8
Enter Hours Day – 3 = 10
Enter Hours Day – 4 = 0
Enter Hours Day – 5 = 9
Enter Hours Day – 6 = 0
Enter Hours Day – 7 = 0
“Usual One!”
Question# 05
Magical Number Table App
Ms. Rimsha, an aspiring magical app developer, has a vision of creating a unique app that
generates multiplication tables with a touch of enchantment. The app will take a user-input
number and produce a table for that number from 1 to 10. Additionally, it will add a magical
touch by checking whether each produced number is even or odd during each iteration.
Here's how the scenario unfolds:
1. User Input:
- App prompts the user to enter a magical number: "Enter a Magical Number:"
2. Table Generation Loop:
- Using a loop, the app generates the multiplication table for the entered number from 1 to
10.
- For each iteration, it checks if the produced number is even or odd.
3. Table Display:
- Display each line of the table with a touch of magic: "Table for [User-Entered Number]:"
- For each iteration, display the multiplication operation and result, along with whether the
result is even or odd.
4. Continue or Exit:
- After displaying the table, the app asks the user if they want to continue or exit: "Do you
want to explore more magical numbers? (Enter 1 or 0):"
- In case of "1", move the control of the program to Start of the Loop.
[OUTPUT SCREEN – 1]
Enter a Magical Number: 5
5 x 1 = 5 (Odd)
5 x 2 = 10 (Even)
5 x 3 = 15 (Odd)
5 x 4 = 20 (Even)
5 x 5 = 25 (Odd)
5 x 6 = 30 (Even)
5 x 7 = 35 (Odd)
5 x 8 = 40 (Even)
5 x 9 = 45 (Odd)
5 x 10 = 50 (Even)
😊 Lots of Luck 😊