CP Assignment # 2 Report
CP Assignment # 2 Report
AIR UNIVERSITY
ASSIGNMENT # 2.
LAB ASSESSMENT:
Data presentation
Experimental results
Conclusion
Date: Signature:
1
Computer Programming Assignment # 2.
Assignment # 2.
Lab Objective:
Understand nesting of if else along with switch.
Task 1:
Write a C++ program that displays a menu (using a switch) that asks the user to select one of the
following choices:
• When first option is selected, prompt the user to enter an integer value. Use a single loop to
count numbers from 1 to that integer value. When the loop is finished, find the average of those
numbers.
• When second option is selected, prompt the user for a number (int). Use a single loop to
calculate the sum of the odd numbers between 1 and that num.
• When third option is selected. Use a single loop to prompt the user to enter random (int) values
and stop whenever the first negative value is entered. Then tell the user how many positive
values have been entered.
• When fourth option is selected. Use NESTED while loops to output all the even numbers from
1 to 80 in 5 rows and 8 columns.
2
Computer Programming Assignment # 2.
• If the user does not select one of these 4 options print "invalid selection".
Solution:
3
Computer Programming Assignment # 2.
Result 1:
4
Computer Programming Assignment # 2.
Result 2:
Explanation:
In this task we have used [int main()]. Main function is used for input and output on the code.
5
Computer Programming Assignment # 2.
3. When third option is selected. Use a single loop to prompt the user to enter random
(int) values and stop whenever the first negative value is entered. Then tell the user how
many positive values have been entered.
4. When fourth option is selected. Use NESTED while loops to output all the even
numbers from 1 to 80 in 5 rows and 8 columns.
And if the user does not select one of these 4 options print "invalid selection".
( return 0 ) is used to end the program.
6
Computer Programming Assignment # 2.
Task 2:
Predict output of the following codes. In each case, show clearly what will be output on the
screen.
Solution:
a) Given code:
int main() {
return 0;
Predicted output:
2, 4, 6, 8, 10
b) Given code:
int main() {
return 0;
}
7
Computer Programming Assignment # 2.
Predicted output:
12
24
36
c) Given code:
int main() {
int x = 5;
while (x > 0) {
x -= 2;
return 0;
Predicted output:
5 3 1
8
Computer Programming Assignment # 2.
Explanation:
in these tasks we will use our knowledge to check the codes and predict there outputs.
Conclusion:
1. We are able to read and understand the code and its output.
2. We can now assign different value and different tasks to different inputs from the user.
3. We can now creat different conditions in nested loops.