0% found this document useful (0 votes)
18 views13 pages

Lab #5 CPP

This document outlines Lab #5 for the ME-237 course at NUST, focusing on iterative structures in programming. It includes objectives, software requirements, and various programming tasks such as calculating factorials, course registration, number guessing, restaurant ordering, and an online quiz system. Each task emphasizes the use of loops, control statements, and user input validation.

Uploaded by

wahabiyi123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Lab #5 CPP

This document outlines Lab #5 for the ME-237 course at NUST, focusing on iterative structures in programming. It includes objectives, software requirements, and various programming tasks such as calculating factorials, course registration, number guessing, restaurant ordering, and an online quiz system. Each task emphasizes the use of loops, control statements, and user input validation.

Uploaded by

wahabiyi123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer and Software Engineering

College of E&ME, NUST, Rawalpindi

ME - 237: Computer System and Programming

LAB # 5: Iterative Structures

Course Instructor: Dr. Asim Khan

Lab Instructor: Engr. Ayesha Khanam

Sr. No. Student’s Name CMS ID

1 ABDUL WAHAB 544532

ME – 237: Computer System & Programming 1


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

LAB # 5: Iterative Structures

Software Requirement(s): MS Visual Studio

Lab Objectives:
In this lab, we will explore iterative structures like while, do-while, and for loops to repeat code
based on specific conditions. The while loop is used when the number of iterations is unknown,
the do-while loop ensures at least one execution, and the for loop is ideal for fixed iterations. We
will also work with nested loops and control statements like break and continue to manage loop
behavior and solve practical programming tasks.

Lab Tasks
1. Write a program that calculates the factorial of a number using a while loop. Ensure that
the user enters a non-negative integer.

#include <iostream>
using namespace std;

int main() {
int n;
long long factorial = 1; // Using long long for large result

cout << "Enter a number: ";


cin >> n;
if (n < 0) {
cout << "Error: Factorial of a negative number doesn't exist.";
} else {
while (n > 0) {
factorial = factorial * n;
n = n - 1;
}
cout << "Factorial = " << factorial;
}

return 0;
}

ME – 237: Computer System & Programming 2


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

2. Extend the Course registration task, from Lab 4 to incorporate multiple registrations
using loops.
3. #include iostream
4. Using namespace std
5. int main()
6. {
7. int capacity = 50;
8. int enrolled;
9. string status;
10. char choice;
11.
12. do
13. {
14.
15. cout << "Enter Statics status (pass/fail): ";
16. cin >> status;
17. for (int i = 0; i < [Link](); i++)
18. {
19. status[i] = tolower(status[i]);
20. }
21.
22. cout << "Enter number of students enrolled in Dynamics: ";
23. cin >> enrolled;
24.
25. if (status != "pass")
26. {
27. cout << "You cannot register for Dynamics.\n";
28. cout << "Reason: Statics prerequisite not cleared.\n";
29. }
30. else
31. {
32. if (enrolled >= capacity)
33. {
34. cout << "Course is FULL.\n";
35. cout << "Suggested Alternative: Register next semester.\n";
36. }
37. else
38. {
39. cout << "Congratulations! You are eligible for Dynamics.\n";

ME – 237: Computer System & Programming 3


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

40. }
41. }
42.
43. cout << "\nDo you want to register another student? (y/n): ";
44. cin >> choice;
45.
46. } while (choice == 'y' || choice == 'Y');
47.
48.
49. return 0;
50. }

3. Write a program where the user has to guess a pseudo-random number (hard coded by you
in the program) between 1 and 100. The program should provide hints such as "Too high" or
"Too low" and stop when the correct number is guessed.

ME – 237: Computer System & Programming 4


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

#include <iostream>
using namespace std;

int main() {
int secret;
cout << "Enter the secret number : ";
cin >> secret;
int guess = 0;

while (guess != secret) {


cout << "Enter your guess: ";
cin >> guess;

if (guess > secret) {


cout << "Too high!" << endl;
}
else if (guess < secret) {
cout << "Too low!" << endl;
}
else {
cout << "Correct! You guessed it." << endl;
}
}

return 0;
}

ME – 237: Computer System & Programming 5


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

ME – 237: Computer System & Programming 6


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

4. Develop a program that simulates a restaurant ordering system. The user should be able to
choose a meal from a menu using a switch-case statement, enter the quantity, and calculate
the total bill. Use a loop to allow multiple orders and an if-else condition for applying
discounts on orders exceeding a specific amount. Calculate and print the complete bill on the
console.

#include <iostream>
using namespace std;

int main()
{
int choice, quantity;
double price = 0, total = 0, discount = 0;
char more;

do
{

cout << "1. Burger - Rs. 500\n";


cout << "2. Pizza - Rs. 1200\n";
cout << "3. Biryani - Rs. 350\n";
cout << "4. Sandwich - Rs. 250\n";
cout << "5. Exit\n";

cout << "Enter your choice: ";


cin >> choice;

if (choice == 5)
break;

cout << "Enter quantity: ";


cin >> quantity;

switch (choice)
{

ME – 237: Computer System & Programming 7


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

case 1:
price = 500;
break;

case 2:
price = 1200;
break;

case 3:
price = 350;
break;

case 4:
price = 250;
break;

default:
cout << "Invalid Choice!\n";
continue;
}

total += price * quantity;

cout << "Item added successfully!\n";

cout << "Do you want to order more? (y/n): ";


cin >> more;

} while (more == 'y' || more == 'Y');

if (total > 3000)


{
discount = total * 0.10;
cout << "\nDiscount Applied: 10%\n";
}

ME – 237: Computer System & Programming 8


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

double finalBill = total - discount;

cout << "Total Amount: Rs. " << total << endl;
cout << "Discount: Rs. " << discount << endl;
cout << "Amount to Pay: Rs. " << finalBill << endl;

return 0;
}

ME – 237: Computer System & Programming 9


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

51. Develop a program that simulates an online quiz system where users answer multiple-
choice questions and receive a score based on their responses. The quiz should consist of
five questions, each with four options (A, B, C, D). A for loop should be used to iterate
through the questions, prompting the user for their answer. A switch-case statement
should then evaluate the selected answer and assign points accordingly. Additionally, an
if-else condition should validate user input to ensure only valid options (A, B, C, or D)
are accepted. At the end of the quiz, the program should display the final score along with
feedback based on performance, for instance, a perfect score (5/5) should display
"Excellent! You got a perfect score. Finally, a while loop should allow users to retake the
quiz by asking if they want to try again. If the user chooses 'Y', the quiz restarts;
otherwise, the program exits.
52. #include <iostream>
53. using namespace std;
54.
55. int main()
56. {
57. char retry;
58.
59. do
60. {
61. int score = 0;
62. char answer;
63.
64.
65.
66. for (int i = 1; i <= 5; i++)
67. {
68. cout << "\nQuestion " << i << ":\n";
69.
70. switch (i)
71. {
72. case 1:
73. cout << "What is the capital of France?\n";
74. cout << "A. Berlin\nB. Madrid\nC. Paris\nD. Rome\n";
75. break;
76.
77. case 2:
78. cout << "Which language is used for C++ programming?\n";
79. cout << "A. HTML\nB. C++\nC. CSS\nD. Python\n";
80. break;
81.
82. case 3:
83. cout << "What is 5 + 3?\n";
84. cout << "A. 6\nB. 7\nC. 8\nD. 9\n";
85. break;
86.
87. case 4:

ME – 237: Computer System & Programming 10


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

88. cout << "Which planet is known as the Red Planet?\n";


89. cout << "A. Earth\nB. Mars\nC. Venus\nD. Jupiter\n";
90. break;
91.
92. case 5:
93. cout << "Who developed C++?\n";
94. cout << "A. Dennis Ritchie\nB. James Gosling\nC. Bjarne Stroustrup\nD. Guido
van Rossum\n";
95. break;
96. }
97.
98. cout << "Enter your answer (A/B/C/D): ";
99. cin >> answer;
100.
101. answer = toupper(answer);
102.
103.
104. if (answer != 'A' && answer != 'B' && answer != 'C' && answer != 'D')
105. {
106. cout << "Invalid option! No points awarded.\n";
107. continue;
108. }
109.
110.
111. switch (i)
112. {
113. case 1:
114. if (answer == 'C') score++;
115. break;
116.
117. case 2:
118. if (answer == 'B') score++;
119. break;
120.
121. case 3:
122. if (answer == 'C') score++;
123. break;
124.
125. case 4:
126. if (answer == 'B') score++;
127. break;
128.
129. case 5:
130. if (answer == 'C') score++;
131. break;
132. }
133. }
134.
135.
136. cout << "Your Final Score: " << score << "/5\n";
137.

ME – 237: Computer System & Programming 11


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

138. if (score == 5)
139. {
140. cout << "Excellent! You got a perfect score.\n";
141. }
142. else if (score >= 3)
143. {
144. cout << "Good Job! You performed well.\n";
145. }
146. else
147. {
148. cout << "Keep practicing. You can do better!\n";
149. }
150.
151. cout << "\nDo you want to try again? (Y/N): ";
152. cin >> retry;
153.
154. } while (retry == 'Y' || retry == 'y');
155.
156.
157. return 0;
158. }

ME – 237: Computer System & Programming 12


Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

ME – 237: Computer System & Programming 13

You might also like