C Program to Find Area of a Circle Triangle and Rectangle

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.

Table Of Contents

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.

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

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

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

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

If you are using Linux/Unix OS and getting an error then read it Getting “undefined reference to sqrt (or other mathematical functions)” even include math.h header?

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply