Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

+10 votes
asked Nov 6, 2021 by Chronic Panda (220 points)
closed Nov 13, 2021 by Admin
closed with the note: answered

5 Answers

0 votes
answered Nov 6, 2021 by Arafat Kabir (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;

}
0 votes
answered Nov 6, 2021 by Areeb Sherjil (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;
}
0 votes
answered Nov 7, 2021 by Dino Vity (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;
}

0 votes
answered Nov 8, 2021 by Punit Gupta (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);
}
0 votes
answered Nov 12, 2021 by sagar kumar (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;

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...