Reputation: 1
what is this code bug? when I enter 50.000 , 50,000 it does not work.
#include <iostream>
using namespace std;
int main()
{
int a,b;
long long int cost;
cin>>a>>b;
cost=a*b;
cout<<cost;
}
Upvotes: 0
Views: 1735
Reputation: 50831
a
and b
are declared as int
. The type of the result a * b
is also int
.
50000 * 50000 exceeds the capacity of an signed int
. That bougous result will then be assigned to the long long int
cost
.
You need to declare a
and b
also as long long int
, then the type of a * b
will also be long long int
and you won't see any problems, provided that the multiplication of a
and b
does not exceed the capacity of long long int
, which still can happend here if you enter really large numbers.
Upvotes: 1