Loading [MathJax]/extensions/MathZoom.js
Skip to main content

C/C++: Mean

Output:

C Codes:


#include<stdio.h>
int main()
{
int i, x;
float y, sum = 0.0;
printf("Enter the Sample Size: ");
scanf("%d", &x);
printf("Enter the Data: ");
for(i = 1; i <= x; ++i){
scanf("%f", &y);
sum += y;
}
float mean = sum/x;
printf("Mean: %2.2f\n", mean);
return 0;
}
view raw mean.c hosted with ❤ by GitHub

C++ Codes:


#include<iostream> // Library for input/output
#include<iomanip> // Library for precision
using namespace std;
int main()
{
int i, x;
float y, sum = 0.0;
cout << "Enter the Sample Size: ";
cin >> x;
cout << "Enter the Data: ";
for(i = 1; i <= x; ++i){
cin >> y;
sum += y;
}
float mean = sum/x;
cout << "Mean: " << setprecision(4) << mean << endl;
return 0;
}
view raw mean.cpp hosted with ❤ by GitHub