Lab Instructor(s) Ahmed Khalid Hooria Najeeb Section BCS-2A1 & 2A2 Semester Spring 2024
Department of Computer Science,
Fast NUCES, Lahore Instructions: • Work in this lab individually. Discussion is not allowed. • Evaluation of tasks will be conducted in the lab. • Anyone caught indulging in the act of plagiarism would be awarded an “F” grade in this lab. Objective: In this lab, students will study: ● Control Structures ● ‘for’ loops ● nested loops Question#1 (10 marks) (for-loop) Write a C++ program that receives a user number and tells you if it is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). In other words, the sum of all positive divisors of the number, excluding the number itself, equals the number. For example: • 6 is a perfect number because its divisors (excluding itself) are 1, 2, and 3, and 1 + 2 + 3 = 6. • 28 is also a perfect number because its divisors (excluding itself) are 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28. Sample:
Question#2 (10 marks)
(nested for-loop) Write a C++ program that displays the following pattern using nested-for loop. Your program should also ask the user for starting and ending point. Sample: Question#3 (10 marks) (for-loop) Build a simple number guessing game using C++. In this game you have to guess a number between your defined range. You enter a lower bound of the range (starting point) and upper bound of the range (ending point). Your program should ask the user “Is your number is this?”. If your number is this then you press “c” and it prints “Yes, You guessed right”. If it is not your number then your program should ask from user whether your number is greater than guessed number or less then guessed number. If you press “h” it means your number is greater. If your number is greater than it adjusts the bound and again ask the user for the guessed number with the new greater values. If you press “l” it means your number is lower. If your number is lower than it adjusts the bound and again ask the user for the guessed number with the new lower values. Your maximum tries is 15. Sample:
Question#4 (10 marks)
(for-loop) Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A loop should then iterate once for each floor. In each iteration, the loop should ask the user for the number of rooms on the floor and how many of them are occupied. After all the iterations, the program should display how many rooms the hotel has, how many of them are occupied, how many are unoccupied, and the percentage of rooms that are occupied. The percentage may be calculated by dividing the number of rooms occupied by the number of rooms. NOTE: It is traditional that most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth iteration. Sample: Question# 5 (10 marks) (nested-for loop) Write a C++ program to print a pattern in the following format:
Sample:
(a) (b)
(c)
NOTE: Your program must work for any input.
Question#6 (10 marks) (nested for-loop) Write a C++ program to display multiplication table from 1 to 10.
NOTE: Your output should be same as shown in given sample