DECLARATION PUZZLE

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
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.
closed account (z05DSL3A)
16 bit int?

Edit:
Your ints 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
@grey Wolf



long int a=(300*300)/300;

using 32 bit int...?

its giving same output 81,




Last edited on
closed account (z05DSL3A)
Don't forget the the literals are still ints.

Try: long int a=(300L * 300L) / 300L;
Topic archived. No new replies allowed.