nightcrawler
Broken In
Ok. I forgot that part as well. Anyways writing the code again wherin I don't round of the value of the number in floor call. Also made changes to printf statement. The program is running as expected on a Linux box minus getch, clrscr calls and conio.h header files and plus the int return format of main. The change to printf statement was to correct the absurd behaviour in linux box wherin the output was not being printed properly. I think this printf should also work in tc/devc++ or any other win32 C compiler
The Output
I think you will find this correct
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
scanf("%Lf",&r);
pi = floor(pi *pow(10, pre)) / pow(10, pre); // this will set the precision according to input value
a = pi * r * r;
printf("The Value of pi taken is %1.*3$Lf and the area of the circle is %Lf\n", pre, pi, a);
getch();
}
Code:
Enter precision of pi between 0 and 8
6
Enter radius
25.3
The Value of pi taken is 3.141592 and the area of the circle is 2010.901623
Enter precision of pi between 0 and 8
4
Enter radius
25.3
The Value of pi taken is 3.1415 and the area of the circle is 2010.842735
Enter precision of pi between 0 and 8
7
Enter radius
25.3
The Value of pi taken is 3.1415926 and the area of the circle is 2010.902007