prev


Assigned Values For Variables

3.Volume of sphere (18.6.2009)

/*
Program# 3
date : 10-12-2008
Volume of sphere
*/

#include <stdio.h>
main(){
float r,p,c;
r=5.0;
p=3.14;
c=4.0/3.0*p*r*r;
printf("\nVolume of sphere : %.2f\n",c);
}

output:
Volume of sphere : 104.66

Things to note:
1. In the computation part, the real value are used (note 4.0 and 3.0). If integer value is used (4/3), then the output will be wrong. Since 4/3 produces result as 1. Because if both operands are int then result will be of type "int".

2. Both / and * operator same precedence so they are evaluated from left to right. stepwise evaluation of the expression is shown below.

c = 4.0/3.0*3.14*5.0*5.0

Expression Operation
c = 1.3333*3.14*5.0*5.0 /
c = 4.186562*5.0*5.0 *
c = 20.93281*5.0 *
c = 104.66 *
next
blog comments powered by Disqus