Press "Enter" to skip to content

Find Average Height of 10 Students in C++ Programming

There are a lot of questions in C++ programming. Here let’s discus a program to find average height of 10 students.

Here we input 10 height entries into the computer and program calculate their sum and average, and display the average on screen. You can do the program with using array and without using array.

Find Average Height of 10 Students Without Array

#include <iostream>
using namespace std;

int main()
{
    int height;
    int height_sum = 0;
    float average;
    for(int i = 1; i<=10; i++)
    {
      cout << "Enter the height for " << i << " student : ";
      cin >> height;
      height_sum = height_sum + height;
    }
    average =  height_sum / 10.0;
    cout<<"Average height is : "<< average;

    return 0;
}

Code Explanation:

Here we simply read 10 numbers as height using a for loop. On each entries, the variable height_sum keeps their sum and finally find average by dividing sum by 10.

Find Average Height of Students Using Array

#include <iostream>
using namespace std;

int main()
{
    int heights[9];
    int height_sum = 0;
    float average;
    for(int i = 0; i<=9; i++)
    {
      cout << "Enter the height of " << i+1 << " student : ";
      cin >> heights[i];
    }
    for(int i = 0; i<=9; i++)
    {
      height_sum = height_sum +  heights[i];
    }
    average =  height_sum / 10.0;
    cout<<"Average height is : "<< average;

    return 0;
}

Code Explanation

Here using a for loop, we keep height entries into an array named height with limit capacity of 10 (0 to 9). And a second for loop calculates sum of each values on array and finally display the average by dividing the sum by 10.

Sample Output

Find Average Height of 10 Students

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *