0% found this document useful (0 votes)
3 views9 pages

CP Assignment # 2 Report

The document outlines Assignment #2 for a Computer Programming course at Air University, focusing on nesting if statements and switch cases. It includes tasks for writing a C++ program with a menu for user interaction and predicting outputs of given code snippets. The assignment emphasizes understanding loops, data types, and conditional statements in programming.

Uploaded by

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

CP Assignment # 2 Report

The document outlines Assignment #2 for a Computer Programming course at Air University, focusing on nesting if statements and switch cases. It includes tasks for writing a C++ program with a menu for user interaction and predicting outputs of given code snippets. The assignment emphasizes understanding loops, data types, and conditional statements in programming.

Uploaded by

awkhan515.69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Programming Assignment # 2.

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

ASSIGNMENT # 2.

Lab Title: Nesting if statements.


Student Name: ABDUL WAHAB Reg. No: 249524
Objective:
Understand nesting of if else along with switch.

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes (5) (4) (3) (2) (1)

Ability to Conduct Experiment

Ability to assimilate the results

Effective use of lab equipment and follows


the lab safety rules

Total Marks: Obtained marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: Signature:

1
Computer Programming Assignment # 2.

Course: CE112L Computer Programming

Assignment # 2.

Submitted To: Mam. Saba Mumtaz

Submitted By: ABDUL WAHAB

Registration number: (249524)

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:

1. Options 'S' or 's'

2. Option 'T' or 't'

3. Options 'P' or 'p'

4. print in the Matrix format

• 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.

 In this task we will be using switch menu.


 In this task we have created integers and used a float, and char data type.
 We have instructed the user to choose one of the following.
1. Options 'S' or 's'
2. Option 'T' or 't'
3. Options 'P' or 'p'
4. print in the Matrix format
 And we have different tasks to be performed at every input such as:
1. 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.
2. 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.

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() {

for (int i = 1; i <= 5; i++) {

cout << i * 2 << " ";

return 0;

Predicted output:

2, 4, 6, 8, 10

b) Given code:

int main() {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 2; j++) {

cout << i * j << " ";

cout << endl;

return 0;

}
7
Computer Programming Assignment # 2.

Predicted output:

12

24

36

c) Given code:

int main() {

int x = 5;

while (x > 0) {

cout << x << " ";

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.

A. In task a we have an integer value 1


 In this task the user has created a for loop to perform its task
 In which he increements his integer value and every time multiply it by 2.

B. In this we are using nested loop


 The I integer represents the outer loop (number of rows).
 The inner loop multiplies the value and print on different lines.

C. in this task we use decreament.


 The integeral value will be first printed
 Then it will minus 2 from the integral value.
 Everytime it minuses 2 from the new value of integer. Until the value reaches 0.

Conclusion:

In this lab task we have learnt:

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.

You might also like