100% found this document useful (1 vote)
195 views

CS201 Lab Exercise

The document describes a coding problem to calculate the average age of 10 students. It outlines declaring 10 integer variables to store each student's age, prompting the user to input the age of each student, and calculating the average by adding all the ages and dividing by 10. The solution uses a for loop to prompt the user for each age, adds it to a running total, then divides the total by 10 after the loop to display the average age.

Uploaded by

Fatima Khan
Copyright
© © All Rights Reserved
100% found this document useful (1 vote)
195 views

CS201 Lab Exercise

The document describes a coding problem to calculate the average age of 10 students. It outlines declaring 10 integer variables to store each student's age, prompting the user to input the age of each student, and calculating the average by adding all the ages and dividing by 10. The solution uses a for loop to prompt the user for each age, adds it to a running total, then divides the total by 10 after the loop to display the average age.

Uploaded by

Fatima Khan
Copyright
© © All Rights Reserved
You are on page 1/ 2

Lab # 1

Week = 26-April -- 30-April-2021

Problem Statement:

“Calculate the average age of a class of ten students. Prompt the user to enter the age of each student.”

 We need 10 variables of int type to calculate the average age.


int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;

 “Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin>> age1;

 Average can be calculated by doing addition of 10 variables and dividing sum with 10.

TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 +age9 + age10 ;

AverageAge = TotalAge / 10;

Solution:

#include<iostream>

using namespace std;

main() {

int age=0;

int TotalAge = 0, AverageAge = 0;

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

cout<<"please enter the age of student "<<i<<" :\t";

cin>>age;
TotalAge += age;

AverageAge = TotalAge/10;

//Display the result (average age)

cout<<"Average of students is: "<<AverageAge;

You might also like