Professional Documents
Culture Documents
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;
return (0);
}
Output:
// 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:-
// 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:-
// 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:-
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:-