×

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.


Related Topics

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.