You are on page 1of 6

Program to calculate Area and Circumference based on user input

Here we are writing a simple C program that calculates the area and circumference of
circle based on the radius value provided by user.

Formula:
Area = 3.14 * radius * radius
Circumference = 2 * 3.14 * radius

To calculate area and circumference we must know the radius of circle. The program will prompt
user to enter the radius and based on the input it would calculate the values. To make it simpler
we have taken standard PI value as 3.14 (constant) in the program. Other details are mentioned
as comments in the below example program.

#include <stdio.h>
int main ()
{
int circle_radius;
float PI_VALUE=3.14, circle_area, circle_circumf;

//Ask user to enter the radius of circle


printf ("\nEnter radius of circle: ");
//Storing the user input into variable circle_radius
scanf ("%d”, &circle_radius);

//Calculate and display Area


circle_area = PI_VALUE * circle_radius * circle_radius;
printf ("\nArea of circle is: %f”, circle_area);

//Calculate and display Circumference


circle_circumf = 2 * PI_VALUE * circle_radius;
printf ("\nCircumference of circle is: %f”, circle_circumf);

return (0);
}
Output:

Enter radius of circle: 2


Area of circle is: 12.560000
Circumference of circle is: 12.560000
It’s time to develop some small programs using operators, printf() and scanf() function.
We will write a C program to find the area of circle, area of a Circle using Symbolic
Constant, area of a Rectangle, area of a right-angle triangle, and area of a triangle
having three sides.

Calculate Area of Circle using C


If the radius of a circle is r then Area is given by ℼ*r2
#include<stdio.h>
int main()
{
// declare variables
float radius, area;

// take inputs
printf("Enter Radius of Circle(in cm): ");
scanf("%f", &radius);

// calculate area
area = 3.14 * radius * radius;

// display result
printf("Area of Circle = %.2f cm\n",
radius,area);

return 0;
}
Output:-

Enter Radius of Circle(in cm): 9.5


Area of Circle = 283.39 cm
Find Area of a Circle Using Symbolic Constant
Here, We will use the symbolic constant to store the value of ℼ, outside of the main()
function.
#include<stdio.h>
#define PI 3.14
int main()
{

// declare variables
float radius, area;

// take input
printf("Enter Radius of Circle(in cm): ");
scanf("%f",&radius);

// calculate area
area = PI * radius * radius;

// display result
printf("Area of Circle = %.2f cm\n",
radius,area);

return 0;
}
Notice that there is no semicolon at the end of a #define line. Value of ℼ taken from the
defined line (PI=3.14). We used %.2f, which will print as a floating point, 2 characters
after decimal points.

Notice that there is no semicolon at the end of a #define line. Value of ℼ taken from the defined
line (PI=3.14). We used %.2f, which will print as a floating point, 2 characters after decimal
points.
C program to find the area of a Rectangle
Area of Rectangle is given by length * width. Here we take len and wid variable to
store the value of length and width respectively.
#include<stdio.h>
int main()
{

// declare variables
float len, wid, area;

// take inputs
printf("Enter length & width of Rectangle (in cm): ");
scanf("%f %f",&len,&wid);

// calculate area
area = len * wid;

// display result
printf("Area of Rectangle= %.3f cm\n",area);

return 0;
}
Output:-

Enter length & width of Rectangle (in cm): 12.5 15.2


Area of Rectangle= 190.000 cm
Enter length & width of Rectangle (in cm): 12.5 15.2
Area of Rectangle= 190.000 cm
C Program to find Area of Right-angle triangle
Area of Right angle triangle or Triangle whose base and height is known, their area is
given by, Area=(1/2) * base * height Or, 0.5 * base * height
#include<stdio.h>
int main()
{
// declare variables
float b, h, area;

// take inputs
printf("Enter base and height (in cm): ");
scanf("%f %f", &b, &h);

// calculate area
area = (0.5 * b * h);

// display result
printf("Area of triangle= %.2f cm\n",
area );

return 0;
}
Output:-

Enter base and height (in cm): 22 5.9


Area of triangle= 64.90 cm

Enter base and height (in cm): 22 5.9


Area of triangle= 64.90 cm
Here we used b and h variables for base and height respectively.
Area of a triangle having three sides
Triangle having three sides their area is given by Heron’s Formula for the area of a
triangle.
If a, b and c are sides of triangles then from Heron’s Formula,

s=(a+b+c)/2
and,
Area=(s*(s-a)*(s-b)*(s-c))1/2
#include<stdio.h>
#include<math.h>
int main()
{

// declare variables
float a, b, c, s, area;

// take inputs
printf("Enter a,b and c value: ");
scanf("%f %f %f",&a,&b,&c);

// calculate are
s = (a+b+c)/2;
area = sqrt( s*(s-a)*(s-b)*(s-c) );

// display result
printf("Area = %.2f\n",area);

return 0;
}
Output:-

Enter a,b and c value: 4.5 8.9 12


Area = 16.64

Enter a,b and c value: 4.5 8.9 12


Area = 16.64
a, b and c are three sides of the triangle. Here we used sqrt() function to find square
root value. sqrt() defined under math.h

You might also like