By 25/02/2014 15 Comments

Program Example # 01 For Arithmetic Operators in C++

Problem statement:

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

Solution (Code Snippet):

/* This program calculates the average age of a class of ten students after prompting the user to enter the age of each student. */
#include <iostream.h>
main ()
{
// declaration of variables, the age will be in whole numbers
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10;
int TotalAge, AverageAge;
// take ages of the students from the user
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 the total age and average age
TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 +
age10;
AverageAge = TotalAge / 10;
// Display the result ( average age )
cout << "Average age of class is: " << AverageAge;
}

Output:

A sample output of the above program is given below.

Please enter the age of student 1: 12

Please enter the age of student 2: 13

Please enter the age of student 3: 11

Please enter the age of student 4: 14

Please enter the age of student 5: 13

Please enter the age of student 6: 15

Please enter the age of student 7: 12

Please enter the age of student 8: 13

Please enter the age of student 9: 14

Please enter the age of student 10: 11

Average age of class is: 12

In the above output the total age of the students is 123 and the actual average should be 12.3 but as we are using integer data types so the decimal part is truncated and the whole number 12 is assigned to the variable AverageAge.







← Previous Tutorial


Posted in: C++ Basics

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .