#include<stdio.h>
int main()
{
int x,y,pxy=1,p26=1,p4x=1,i=1;
float s;
printf("enter the value of x and y");
scanf("%lldll%d",&x,&y);
while(i<=y)
{
pxy=pxy*x;
i=i+1;
}
p26=2*2*2*2*2*2;
i=1;
while(i<=x)
{
p4x=p4x*4;
i=i+1;
}
s=(pxy*p26)/p4x;
printf("the value of s %f",s);
return 0;
}
The table below shows how huge a value can be stored in int.
Type | Storage size | Value range |
---|---|---|
int |
2 bytes 4 bytes |
-32,768 to 32,767 -2,147,483,648 to 2,147,483,647 |
Now consider a numberline from -2,147,483,648 to 2,147,483,647. The value of pxy obtained after running
while(i<=y)
{
pxy=pxy*x;
i=i+1;
}
is far greater than the limit. Therefore, it will keep shuttling back and forth on the number line until the value of pxy is stored somewhere on the number line (i.e. after reaching 2,147,483,647 the 2,147,483,648th value will lie at 2,147,483,646 on the number line). In this case, it was a number on the -ve side, which led to a wrong and -ve answer.
If you still don’t understand, here is an example:
Suppose your number is 7, but the storage value of int extends from -3 to 3. So the values uptill 3 get stored alright, but after that the other values start retracing the path. Therefore ‘4’ gets stored at the position of ‘2’, ‘5’ at ‘1’, ‘6’ at ‘0’ and ‘7’ at ‘-1’. Thus, the value of 7 becomes equal to ‘-1’.
I hope this was helpful.