-1

on changing this expression to postfix representation and then evaluating it.. I'm getting 17.8 as answer.. if "n" would have been of float type then there is no problem in answer. since n is a variable of integer type, digits after decimal oint should be truncated and answer should be 17 but the output is 15. how?

#include<stdio.h>
int main()
{

int i=3,a=4,n;
float t =4.2;
n=a*a/i+i/2*t+2+t;
printf("n = %d\n",n);
return(0);
}

1 Answers1

0

I'm getting 17.8 as answer.. if "n" would have been of float type then there is no problem in answer.

No. You can't. You will get 17.833332 if your expression would be

n=a*a*1.0/i+i/2.0*t+2+t;  

See:

a*a/i = (4*4)/3 = 16/3 = 5 
i/2*t = (3/2)*4.2 = 1*4.2 = 4.2
2 + t = 2 + 4.2 = 6.2  

5 + 4.2 + 6.2 = 15.4 which will truncated to 15.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • can u please explain the logic for this 1 as well... float a=1.5; int b=3; a=b/2+b*8/b-b+a/3; – user3725723 Jun 14 '14 at 12:51
  • @user3725723; If you got the answer then you should be able to do that for yourself. Apply same logic. – haccks Jun 14 '14 at 12:57
  • I tried but I'm getting 7 as answer.. but output is 6.500000 c postfix notation is b2/b8*b/+b-a3/+ => {(b/2)+[(b*8)/b]}-b+(a/3) =>{(1.5)+[(24)/3]}-3+(.5) =>{(1.5)+8}-2.5 = 7 Do I need to evaluate b/2 as 1 (I assumed so because b is an integer) but the variable, that we are evaluating is a float value then b/2 should be equal to 1.5! – user3725723 Jun 14 '14 at 13:18