why is this giving the right answer but adding a 0 to the end of the answer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int main ()
{
  int n1, n2, n3;

  cout << "Enter three numbers to compare them: ";
  cin >> n1 >> n2 >> n3;

int sum;
  sum = n1 +  n2 + n3;
  cout << "Sum is " << sum << endl;

int average;
average = n1 + n2 + n3 / 3;
cout <<"Average is " << average << endl;

int product;
product = n1 * n2 * n3;
cout << "Product is " << product << endl;

int smallest;
  if(n1 < n2 && n1 < n3)
    cout << "Smallest is " << n1 << smallest;

  if(n2 < n3 && n2 < n1)
    cout << "Smallest is " << n2 << smallest;

  if(n3 < n1 && n3 < n2)
    cout << "Smallest is " << n3 << smallest;


}
Last edited on
Your calculation for the average is incorrect. You want to sum them, then divide by 3, not divide the third by 3 then add the first two. Check your order of operations.
average = n1 + n2 + n3 / 3;

take a closer look at that. Remember that / has a higher priority than +, so the above is the same as average = n1 + n2 + (n3 / 3); which isn't what you want.

Also:

1
2
  if(n1 < n2 && n1 < n3)
    cout << "Smallest is " << n1 << smallest;


Why are you printing 'smallest' here? You never set 'smallest' to anything.
cout<< average / 3;
and its better to compare your variables with a "for" & "if"
if you want to learn try to write and debug you yourself.
try to solve your problems by yourself.
Topic archived. No new replies allowed.