1012 Views

How to write c++ program to find average of n numbers?


Sahithi K 1st Sep, 2019
Answer (1)
rajaman9300 Student Expert 1st Sep, 2019

Hello Sahithi

#include <iostream>

using namespace std;

int main()

{

    int n, i;

    float num[100], sum=0.0, average;

    cout << "Enter the numbers of data: ";

    cin >> n;

    while (n > 100 || n <= 0)

    {

        cout << "Error! number should in range of (1 to 100)." << endl;

        cout << "Enter the number again: ";

        cin >> n;

    }

    for(i = 0; i < n; ++i)

    {

        cout << i + 1 << ". Enter number: ";

        cin >> num[i];

        sum += num[i];

    }

    average = sum / n;

    cout << "Average = " << average;

    return 0;

}

Related Questions

Amity University, Noida : MBA...
Apply
Ranked among top 10 B-Schools in India by multiple publications | Top Recruiters-Google, MicKinsey, Amazon, BCG & many more.
Amity University Noida B.Tech...
Apply
Among top 100 Universities Globally in the Times Higher Education (THE) Interdisciplinary Science Rankings 2026
UPES B.Tech Admissions 2026
Apply
Ranked #43 among Engineering colleges in India by NIRF | Highest Package 1.3 CR , 100% Placements
Amity University, Noida | Law...
Apply
Among top 100 Universities Globally in the Times Higher Education (THE) Interdisciplinary Science Rankings 2026
XIME Bangalore PGDM Admission...
Apply
Accredited by AICTE & NBA | Highest CTC 14.7 LPA | Average CTC 10.5 LPA | Key Recruiters: EY, Deloitte, KPMG, HCL
UPES Integrated LLB Admission...
Apply
Ranked #18 amongst Institutions in India by NIRF | Ranked #1 in India for Academic Reputation by QS Rankings | 16 LPA Highest CTC
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books