C program to find area of circle

In this program, we are going to find area of circle using given formulas and implement it through c language.
First, we will see concepts:

Area of circle:

area of circle
All area inside circle in filled with light blue colour is area of circle. To find out the area of circle value, standard formula used is:
Area of circle = pi * (r)2

Here, pi is constant whose standard value is 3.14 and r is radius of circle.

circle radius

As you can see in figure, half line in circle depicts the radius of circle. This is brief to our concepts that we are going to learn in this program.
Now, to implement above formula in c program, we need to use PI constant in our program and radius, we can input that through command i.e. user input.

Example:

example

Input: 5
Area of circle = pi * r2 = 3.14 * (5)2   = 3.14 * 25 = 78.5
Output = 78.5
So, answer is 78.5

Pre-requisites:

To be able to write this program, knowledge of basics, c program structure, main method, data types and variables, input output function is necessary. These are basic concepts which are implemented almost in every program.

Important for this program:

Input/output function to accept and print output, main method for program execution, variables to store values and data types to declare variable type.

Step by step approach:

  • First, define constant pi before main function. Then create variable radius to store radius of circle which will be then used in area of circle formula.
  • Second, declare float variable, area of circle, to store the resulting output value from calculation. We can either use float or long data type while declaring it depending upon range of input value. If input value is large number (two digit or three digit), we need to declare as long type for output variable (area of circle).
  • Third, display output value using printf function.

Here, \n is used in program to move cursor to new line after each input output statement. It is also called as escape sequence character.

Program

C program to find area of circle

#include<stdio.h>
int main()
{
  int radius_of_circle; //declare variable to accept user input
  float area_of_circle; //declare variable to store output value
  printf("\n Enter the circle radius:");
  scanf("%d",&radius_of_circle); // accept user input
  area_of_circle = pi * radius_of_circle * radius_of_circle; // calculate area of circle
  printf("\n Area of circle will be %f", area_of_circle); //  display are of circle
  return 0; 
}  

Output

Enter the circle radius: 5
Area of circle will be 78.500000

Program Explanation:

In this program, we have used standard formula to calculate area of circle. Formula uses pi constant so that we have defined pi as constant and set it value 3.14 before main method begins.

#define pi 3.14

 

We have declared two variables to store input (radius_of_circle) and display output (area_of_circle) respectively. Here in this program, we have used integer data type while declaring radius variable because we have entered integer input in the program example. So if you want to input any floating value then float variable is mandatory otherwise output will be inaccurate.

Variable, area_of_circle is declared of float data type because output value will be decimal value/floating value. If input is large number then we can declare area_of_circle variable as long data type because it can hold larger floating value.

After calculation of area of circle, area_of_circle = pi * radius_of_circle * radius_of_circle;
We have displayed the output value using printf function at end of program and then return 0 statement added to return 0 value indicating successful program execution of main method.

If you have any doubt leave comment below post.

Keep coding.

Leave a Reply

error: Content is protected !!
%d bloggers like this: