0% found this document useful (0 votes)
5 views3 pages

Calculate Average Age of Students

The document outlines a lab exercise focused on calculating the average age of ten students using C++. It details the necessary variables, user input prompts, and provides two solutions: one using individual variables for each student's age and another utilizing a loop for input. The average is computed by summing the ages and dividing by ten.

Uploaded by

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

Calculate Average Age of Students

The document outlines a lab exercise focused on calculating the average age of ten students using C++. It details the necessary variables, user input prompts, and provides two solutions: one using individual variables for each student's age and another utilizing a loop for input. The average is computed by summing the ages and dividing by ten.

Uploaded by

ibrahimkhan2p123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab # 1

Week = 21st-October – 25th-October-2024

Topics Covered:

 Variables
 Data Types
 Arithmetic Operators
 Precedence of Operators

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;

int main(){

// declaring 10 integer variables to take input of students age


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

// declaring variables to calculate totalAge and averageAge


int TotalAge = 0, AverageAge = 0;

// taking input of each student age


cout<<"please enter the age of student 1 ";
cin>>age1;

cout<<"please enter the age of student 2 ";

cin>>age2;

cout<<"please enter the age of student 3 ";

cin>>age3;

cout<<"please enter the age of student 4 ";

cin>>age4;

cout<<"please enter the age of student 5 ";

cin>>age5;

cout<<"please enter the age of student 6 ";

cin>>age6;

cout<<"please enter the age of student 7 ";

cin>>age7;

cout<<"please enter the age of student 8 ";

cin>>age8;

cout<<"please enter the age of student 9 ";

cin>>age9;

cout<<"please enter the age of student 10 ";

cin>>age10;

//calculate totalAge by adding age of all students


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

// calculate AverageAge by dividing the totalAge with number of students


AverageAge = TotalAge/10;

//Display the result (average age)

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

}
Alternative Solution

#include<iostream>

using namespace std;

main() {
// declaring variable age to take input
int age=0;

// declaring variables to calculate totalAge and averageAge


int TotalAge = 0, AverageAge = 0;

// using for loop to take input of each students and adding them
for (int i = 1;i<=10;i++){

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

cin>>age;

TotalAge += age;

// calculate AverageAge by dividing the totalAge with number of students


AverageAge = TotalAge/10;

//Display the result (average age)

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

You might also like