# Write a complete C++ program that prompts 4 whole numbers as input, calculates and display average of them.

asked Nov 6, 2021
closed Nov 13, 2021
closed with the note: answered

answered Nov 6, 2021 by (140 points)
#include<iostream>

using namespace std;

int main(){

int a,b,c,d, sum=0;  //(declaring variable)

float average;

cout<<"First number:  ";

cin>>a;

cout<<endl;

cout<<"Second number:  ";

cin>>b;

cout<<endl;

cout<<"Third number:  ";

cin>>c;

cout<<endl;

cout<<"Fourth number:  ";

cin>>d;

cout<<endl;

sum=a+b+c+d;

average=(sum*1.00)/4;

cout<<"The average of 4 numbers is: "<<average;

return 0;

}
answered Nov 6, 2021 by (1,920 points)
edited Nov 9, 2021 by Areeb Sherjil
#include <iostream>
#include<vector>

class Average
{
public:
Average();
private:
std::vector<double>storage;
double avg{};
};

Average::Average()
{
int count{};double temp{};
std::cout<<"How many numbers do you want to enter?\n";
std::cin>>count;

for (size_t i{};i<count;++i)
{
std::cout<<"Enter a number:\n";
std::cin>>temp;storage.push_back(temp);
avg+=temp;
}

std::cout<<"The average is: "<<avg/storage.size();
}

int main()
{
Average start1;
return 0;
}
answered Nov 7, 2021 by (740 points)

For this question you just need the sum of the input numbers, which can be calculated by cumulative addition within a for-loop.

#include <iostream>
int main()
{
int sum = 0;
for (int i = 0; i < 4; ++i) {
int n;
std::cout << "Whole number? ";
std::cin >> n;
sum += n;          // <<< cumulative addition here
}
std::cout << "The average is " << sum/4.0 << std::endl;
// note: 4.0 used to prevent "integer division" that returns quotient only.

return 0;
}

answered Nov 8, 2021 by (140 points)
#include <iostream>
using namespace std;
void calcAvg(double a , double b , double c , double d)
{
double sum = a+b+c+d;
double avg = sum/4;
cout<<"Average of the 4 entered numbers is = "<<avg;
}
int main()
{
double n1 , n2 , n3 , n4;
cout<<"Enter 4 whole numbers\n";
cin>>n1;
cin>>n2;
cin>>n3;
cin>>n4;
calcAvg(n1,n2,n3,n4);
}
answered Nov 12, 2021 by (140 points)
#include<iostream>

using namespace std;

int main() {

int a,b,c,d;

cin>>a;

cin>>b;

cin>>c;

cin>>d;

cout<<(a+b+c+d)/4;

return 0;

}