0

i'm having a problem with this operation that is not really giving the right result. The result is 60216 on terminal, but it should be 563376.

int a = 8536;
int b = 563376;
int d = 8536;
unsigned long long int k = (a*b);
cout << k/d << endl;
Leonardo
  • 49
  • 7

1 Answers1

1

you need long long everywhere

long long int a = 8536;
long long int b = 563376;
long long int d = 8536;
unsigned long long int k = (a * b);
std::cout << k / d << std::endl;

note that its nothing to do with division. THis

int a = 8536;
 int b = 563376;
unsigned long long int k = (a * b);
std::cout << k  << std::endl;

gives the wrong answer too

pm100
  • 48,078
  • 23
  • 82
  • 145
  • I tryed to put only b as long long int and it worked. Do you know why ? – Leonardo Feb 16 '22 at 00:30
  • @Leonardo https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules – pm100 Feb 16 '22 at 00:31
  • @Leonardo summary: types get promoted to the longest one in a given operation. SO having one long long in a calculation promotes them all to long long – pm100 Feb 16 '22 at 00:32
  • Oh, thanks! I Have spent so much time trying to fix that. You helped me a lot. Thanks again. – Leonardo Feb 16 '22 at 00:35