Pi() Function in C
Pi:
Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and math.h libraries.
To use the pi function in c:
One of the following methods is used for using the preprocessor directives as “#define” to make the “PI” value is equals to the 3.142857.
Let’s see #define PI as 3.14 in c:
Assuming that you express "#define PI 3.14", all examples of PI in the program will be supported with 3.14 by the preprocessor before aggregate time.
Assuming that you express "const float PI=3.14", 3.14 will be put away in memory held for the consistent PI and utilized at run time.
M_PI in c:
Pi, the ratio of circle's circumference to its diameter. Pi is divided by two.
Press ctrl + shift + u then type zero-three-C-zero,
then, at that point, press enter and you get the pi image. How would I type the Pi image on Windows 10 in the event that I don't have NUM lock?
You needn't bother with a NUM lock; all things considered, simply hold down the alt key and type 227. The 227 must be from the console on the right half of your console.
The PI constant is there in the cmath header file.
The name of the consistent is M_PI.
We can essentially incorporate that header document, and utilize the steady to perform activity.
Syntax for pi:
#define PI (3.141592653589793)
Let’s see an example program for pi using c:
// to find the radius of circle
#include <stdio.h>
void test()
{
float pi = 22.0f/7.0;
printf("float \n");
printf("%.25lf\n", pi);
printf("%lf\n", pi);
}
void test1()
{
double pi = 22.0/7.0;
printf("doublen");
printf("%.25lf\n", pi);
printf("%lf\n", pi);
}
void test2()
{
long double pi = 22.0L/7.0;
printf("long double \n");
printf("%.25Lf\n", pi);
printf("%Lf\n", pi);
}
int main(void)
{
test();
test1();
test2();
return 0;
}
Output:
float
3.142857074737
3.142857
double
3.14285714285714
3.142857
long double
3.142857142857
3.142857
Let’s see another example on pi function in c:
#include <stdio.h>
#define PI 3.1
int main()
{
int radius;
float area;
printf("Enter the radius of the circle\n");
scanf("%d", &radius);
area = PI*radius*radius;
printf("Area of the circle is %.2f\n", area);
return 0;
Output:
Enter radius of the circle: 2
Area of circle: 18.1565
Just thing that you have to observe that in our program pi is supported by the 3.14 at whatever point it came in the program even prior to ordering. In this way, PI*radius*radius will be
3.14*radius*radius.
let’s see another to print area of circle using marcos:
#include <stdio.h>
#define area(r) (3.14*r*r)
int main()
{
int radius;
float area;
printf("Enter the radius of the circle\n");
scanf("%d", &radius);
area = area(radius);
printf("Area of the circle is %.2f\n", area);
return 0;
}
Output:
Enter radius of the circle
3
Area of circle is 27.54
We have characterized a large scale 'region' which takes one contention that is 'r'. In this way, when we called area(radius), it got supplanted by 3.14*radius*radius (as area(r) is 3.14*r*r).
Not at all like functions which are called during compilation, any place there is area(x) in our code it will be straight forwardly supported by 3.14*x*x before compilation.
Let's Compare Macro with Function
We have recently seen a guide to print the region of a circle utilizing macros. Presently we should see a similar model utilizing function.
#include <stdio.h>
float CircleArea(int r)
{
float a;
a = 3.14*r*r;
int radius;
return a;
}
int main ()
{
int radius;
float area;
printf("Enter radius of the circle\n");
scanf("%d", &radius);
area = CircleArea(radius);
printf("Area of circle is %.2f\n", area);
return 0;
}
Output:
Enter radius of the circle
2
Area of circle is 18.24
The primary contrast is that macros support the codes by their access (as it area(r) with (3.14*r*r)).
Along these lines, each time code will be distributed some space. This implies that each time area(r) will show up, it will be space distributed in the memory. Yet, this isn't true with function.
By this way we use the pi function in the programming languages. Hence the pi function using macros also be useful.