Lab Work (Repetition)
Lab Work (Repetition)
For question no 1 and 2, trace manually the for statement and give the output.
Answer:
Answer:
Answer:
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>
int main()
int i,n,sum=0;
float avg;
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