0% found this document useful (0 votes)
104 views

Lab Work (Repetition)

The document contains 4 exercises on loops in C++. Exercise 1 has 4 parts asking to trace for loops and output values. Exercise 2 has 2 parts tracing a for loop and determining output. Exercise 3 provides code to read 10 numbers, calculate total and average using a for loop. Exercise 4 outlines 4 tasks to perform using while loops, including displaying numbers, even numbers, and odd numbers between two user-input integers.

Uploaded by

Syahirah Zaki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Lab Work (Repetition)

The document contains 4 exercises on loops in C++. Exercise 1 has 4 parts asking to trace for loops and output values. Exercise 2 has 2 parts tracing a for loop and determining output. Exercise 3 provides code to read 10 numbers, calculate total and average using a for loop. Exercise 4 outlines 4 tasks to perform using while loops, including displaying numbers, even numbers, and odd numbers between two user-input integers.

Uploaded by

Syahirah Zaki
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Exercise Repetition

For question no 1 and 2, trace manually the for statement and give the output.

1. Trace the following program fragment

i) for (int x=1;x<=9;x=x+2)


cout << x;

Answer:

ii) for (int x=5;x>-6;x--)


cout << x;

Answer:

iii) for (int x=3;x>=-6;x=x-3)


cout << x;

Answer:

iv) for (int x=4;x<8;x++)


cout << (x*2);

Answer:

1
2. Trace the following program fragment and determine the output.

a) int x = 10
for (int m=-5;m<=10;m=m+5)
{
x=x+m;
}
cout << x;

Answer:

b) int y = 10
for (int m=-5;m<=-10;m=m-5)
{
y=y-m;
}
cout << y;

Answer:

3. Write a program that reads 10 integer numbers and then find the total and average of
the numbers (you may use for or while loop)

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

int i,n,sum=0;

float avg;

cout << " Enter 10 numbers \n" ;

for (i=1;i<=10;i++)

cout << "\n Enter number " << i << " : ";

cin >> n ;

2
sum +=n;

avg=sum/10.0;

cout << "\n The sum of 10 no is : " << sum << "\n
The average number is : " << avg ;

4. Write a program that uses while loops to perform the following tasks:

i. Ask the user to input two integers, firstnum and secondnum (firstnum must be
less than secondnum)
ii. Display all numbers between firstnum and secondnum
iii. Display even numbers between firstnum and secondnum
iv. Display odd numbers between firstnum and secondnum

You might also like