Make a program that will ask the users to input 4 different numbers. After the userâ€™s input the program will display the formula on the next line. Next line would be the presentation of the formula which the variables were substituted by the inputs from the user, then returns the average of the numbers entered.

**All inputs are integers except average which is float with two decimal places.
How would you like us to help? We prefer to avoid giving solutions to problems here, just nudges in the right direction. :)

-Albatross
what formula will display on the next line does it mean ? I'm just rookie in programming . I cannot understand further .
I'm guessing you were given a formula that you need to implement. Something like c = a + b or c = a^(b+C). If not, then you have an ambiguous prompt and should talk to whoever gave it to you.

However, if I were to make a complete shot in the dark that I may be completely wrong about, the formula they want you to display and implement is for a 4-value mean average ((a+b+c+d)/4, note that copy-pasting this into your program will not produce a desired result). The reason I think this is because you are asked to have a floating-point (decimal) variable named "average" and the program is supposed to return the average of the numbers entered.

-Albatross
Is this correct ?

#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
float num1,num2,num3,num4,result;

cout<<"Enter 1st digit: ";
cin>>num1;
cout<<"Enter 2nd digit: ";
cin>>num2;
cout<<"Enter 3rd digit: ";
cin>>num3;
cout<<"Enter 4th digit: ";
cin>>num4;

cout<<num1<<num2<<num3<<num4;

result=num1+num2+num3+num4/4;
cout<<result;

getch();
}

 **All inputs are integers except average which is float with two decimal places.

This means that:

 ``123456`` ``````float num1,num2,num3,num4,result; // should be int num1, num2, num3, num4; float result;``````

The way you compute the result is wrong.
You should remember from mathematics that multiplication and division have precedence over addition and subtraction.

This means that, for example:

 ```2 + 3 * 10 == 2 + 30 == 32 (2 + 3) * 10 == 5 * 10 == 50```

So the correct way to compute the average is:

`result = (num1 + num2 + num3 + num4) / 4;`

Finally, you must display the formula. You currently only display the numbers (and without spaces between them).

 ``12`` ``````cout << "Formula is:\n"; cout << "( " << num1 << " + " << num2 << " + " << num3 << " + " << num4 << " ) / 4\n";``````

Last edited on
Topic archived. No new replies allowed.