Calculate Value of Pi up to 2,3,4,5 decimal places using c program
#include<stdio.h>
#include<math.h>
void main()
{
float i= 4.0;
int itr=1;
int j=3;
while(fabs(i-3.14000f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("Irt for 3.14 =%d\n",itr);
itr=1,j=3,i = 4.0;
while(fabs(i-3.14100f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("Itr for 3.141=%d\n",itr);
itr=1,j=3,i=4.0;
while(fabs(i-3.14150f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("itr for 3.1415=%d\n",itr);
itr=1,j=3,i=4.0;
while(fabs(i-3.141590f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("itr for 3.14159=%d\n",itr);
}
#include<stdio.h>
#include<math.h>
void main()
{
float i= 4.0;
int itr=1;
int j=3;
while(fabs(i-3.14000f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("Irt for 3.14 =%d\n",itr);
itr=1,j=3,i = 4.0;
while(fabs(i-3.14100f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("Itr for 3.141=%d\n",itr);
itr=1,j=3,i=4.0;
while(fabs(i-3.14150f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("itr for 3.1415=%d\n",itr);
itr=1,j=3,i=4.0;
while(fabs(i-3.141590f)>0.00001f)
{
i=i + pow(-1,itr)*(4.0/j);
//printf("%f\n",i);
itr++;
j=j+2;
}
printf("itr for 3.14159=%d\n",itr);
}
No comments:
Post a Comment