C Program to Find the Perimeter of a Circle, Rectangle and Triangle

«
»

This is a C Program to find the perimeter of a circle, rectangle and triangle.

Problem Description

This C Program calculates the perimeter of a circle, rectangle and triangle.

Problem Solution

This program is used to find the perimeter of a circle, rectangle and triangle. The formula used in this program are
perimeter of rectangle: 2 * (a + b)
perimeter of General triangle: a + b + c
perimeter of Equilateral triangle: 3 * a
perimeter of Right angled triangle: width + height + sqrt(width ^ 2 + height ^ 2)
perimeter of circle: 2 * pi * r

advertisement
Program/Source Code

Here is source code of the C Program to Find the perimeter of a circle, rectangle & triangle.The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Find the Perimeter of a Circle, Rectangle and Triangle
 */
#include <stdio.h>
#include <math.h>
 
int main()
{
    float radius, length, width, a, b, c, height;
    int n;
    float perimeter;
 
    //Perimeter of rectangle
    printf(" \n Perimeter of rectangle \n");
    printf("---------------------------\n");
    printf("\n Enter width and length of the rectangle : ");
    scanf("%f%f", &width,& length);
    perimeter = 2 * (width + length);
    printf("Perimeter of rectangle is: %.3f", perimeter);
 
    //Perimeter of triangle
    printf("\n Perimeter of triangle n");
    printf("---------------------------n");
    printf("\n Enter the size of all sides of the triangle : ");
    scanf("%f%f%f", &a, &b, &c);
    perimeter = a + b + c;
    printf("Perimeter of triangle is: %.3f", perimeter);
 
    //Perimeter of circle
    printf(" \n Perimeter of circle \n");
    printf("---------------------------\n");
    printf("\n Enter the radius of the circle : ");
    scanf("%f", &radius);
    perimeter = 2 * (22 / 7) * radius;
    printf("Perimeter of circle is: %.3f", perimeter);
 
    //Perimeter of equilateral triangle
    printf(" \n Perimeter of equilateral triangle \n");
    printf("---------------------------\n");
    printf("\n Enter any side of the equilateral triangle : ");
    scanf("%f", &a);
    perimeter = 3 * a;
    printf("Perimeter of equilateral triangle is: %.3f", perimeter);
 
    //Perimeter of right angled triangle
    printf(" \n Perimeter of right angled triangle \n");
    printf("---------------------------\n");
    printf("\n Enter the width and height of the right angled triangle : ");
    scanf("%f%f", &width, &height);
    perimeter = width + height + sqrt(width * width + height * height);
    printf("Perimeter of right angled triangle is: %.3f", perimeter);
    return 0;
}
Program Explanation

This C program is used to find the perimeter of a circle, rectangle and triangle. We are reading the value for ‘width’ and ‘length’ variables respectively. Compute the perimeter of a rectangle. The following formula is used

Perimeter = 2* (width + length).

advertisement

We are reading the values for ‘a’, ’b’, ‘c’ variables respectively. Compute the perimeter of the triangle, the following the formula is used.

Perimeter = a + b + c.

We are reading the value for ‘radius’ variable. Compute the perimeter of circle, the following formula is used

Perimeter = 2 * (22/7) * radius.

advertisement

We are reading the value for ‘a’ variable. Compute the perimeter of a equilateral triangle, the following formula is used.

Perimeter = 3 * a.

We are reading the values for ‘width’ and ‘height’ variables respectively. Compute the
perimeter of Right angled triangle, the following formula is used

Perimeter = width + height + sqrt((width * width) + (height * height)).

advertisement
Runtime Test Cases
 
Output:
$ cc pgm32.c -lm
$ a.out
 Perimeter of rectangle
---------------------------
 
Enter width and length of the rectangle : 12 13
Perimeter of rectangle is: 50.000
 
Perimeter of triangle
---------------------------
 
Enter the size of all sides of the triangle : 12 16 18
Perimeter of triangle is: 46.000
 
Perimeter of circle
---------------------------
 
Enter the radius of the circle : 10
Perimeter of circle is: 60.000
 
Perimeter of equilateral triangle
---------------------------
 
Enter any side of the equilateral triangle : 19 34
Perimeter of equilateral triangle is: 57.000
 
Perimeter of right angled triangle
---------------------------
 
Enter the width and height of the right angled triangle : 5 7
Perimeter of right angled triangle is: 73.366

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn