Declaration of negative values

Hello guys,
My program works just fine but when I input negative integers it brings wrong output

If product and sum of A and B it should print Gotcha
If sum is greater than product it should print Sum
If product is greater than sum it should print Product


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
#include<iostream>
using namespace std;
int main()
{
  int sum, product;
  float a , b;
  cout <<"Enter value of a:" <<endl;
  cin >> a ;
  cout << "Enter value of b:" <<endl;
  cin >> b ;
  sum= a + b;
  product= a * b;
  if (sum==product)
  {
    cout << "GOTCHA!";
  }
  else if (sum > product)
  {
    cout <<"SUM";
  }
  else if (product > sum)
  {
    cout << "PRODUCT";
  }
 }
Last edited on
Use int a, b; and not float a, b; to avoid errors associated with the finite accuracy of the latter and the truncation used to produce sum and product.

When you have changed that, please us give us examples of input/output that you think are giving the wrong result.
Last edited on
Thanks I've already solved it
Topic archived. No new replies allowed.