Jul 14, 2011 at 1:14pm UTC
hi folks,
i am new here,i know some basics in c++.
i would like know answer for this question..?
1 2 3 4 5 6 7 8
#include<iostream.h>
#include<conio.h>
void main()
{
int a=(300*300)/300;
cout<<a;
getch();
}
OUTPUT
value of a is 81 (in BORLAND c or c++ compiler )
CAN ANYBODY EXPLAIN WHY...?
Last edited on Jul 14, 2011 at 1:15pm UTC
Jul 14, 2011 at 1:35pm UTC
Hmm, interesting, mine says 300 - and why would you calculate something like that anyway? You might as well just put 300 there. For test purpose, perhaps?
IsnĀ“t Borland C/C++ compiler obsolete? Try Code::Blocks. I use it & it's good compiler. That's all I would recommend doing now.
Jul 14, 2011 at 1:46pm UTC
16 bit int?
Edit:
Your int s are 16bit. when you do (300*300) you overflow the int, leaving a value of 24300 then devide that by 300 to give 81.
Last edited on Jul 14, 2011 at 1:52pm UTC
Jul 14, 2011 at 4:17pm UTC
@grey Wolf
long int a=(300*300)/300;
using 32 bit int...?
its giving same output 81,
Last edited on Jul 14, 2011 at 4:18pm UTC
Jul 14, 2011 at 4:52pm UTC
Don't forget the the literals are still int s.
Try: long int a=(300L * 300L) / 300L;