Write a c program to find the perimeter of a circle, rectangle and triangle

Formula of perimeter of different shapes in geometry:

1. Square = 4 * a
2. Rectangle: 2 * (a + b)
3. General triangle: a + b + c
4. Equilateral triangle: 3 * a
5. Right angled triangle: w + l + √ (w2 + l2)
6. Circle: 2 * π * r
7. Parallelogram: 2 * (a + b)
8. Rhombus: 4 * a
9. Cube: 12 * a
10. Cuboids: 4 * (w + l + h)
11. Trapezium: a + b + c + d
12. Equilateral polygon: n * a
13. Regular polygon: 2 * n * a sin (π /n)
14. General polygon: a1 + a2 + a3 + … + an

C code:


#include<stdio.h>
#include<math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

int main(){

    float r,l,w,h,a,b,c;
    int n;
    float perimeter;

    //Perimeter of square
    printf("Enter the size of any side square : ");
    scanf("%f",&a);
    perimeter = 4 * a;
    printf("Perimeter of square is: %.3f",perimeter);

    //Perimeter of rectangle
    printf("\n\nEnter width and length of the rectangle : ");
    scanf("%f%f",&w,&l);
    perimeter = 2 * (w +l);
    printf("Perimeter of rectangle is: %.3f",perimeter);

    //Perimeter of triangle
    printf("\n\nEnter 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\nEnter the radius of the circle : ");
    scanf("%f",&r);
    perimeter = 2 * M_PI * r;
    printf("Perimeter of circle is: %.3f",perimeter);

    //Perimeter of cuboids
    printf("\n\nEnter width, length and height of the cuboids : ");
    scanf("%f%f%f",&w,&l,&h);
    perimeter = 4 * (w + l + h);
    printf("Perimeter of cuboids is: %.3f",perimeter);

    //Perimeter of cube
    printf("\n\nEnter any side of the cube : ");
    scanf("%f",&a);
    perimeter = 12 * a;
    printf("Perimeter of cube is: %.3f",perimeter);

    //Perimeter of rhombus
    printf("\n\nEnter any side of the rhombus : ");
    scanf("%f",&a);
    perimeter = 4 * a;
    printf("Perimeter of rhombus is: %.3f",perimeter);

    //Perimeter of parallelogram
    printf("\n\nEnter size of any  two consecutive side of parallelogram : ");
    scanf("%f%f",&a,&b);
    perimeter = 2 *(a + b);
    printf("Perimeter of parallelogram is: %.3f",perimeter);

    //Perimeter of equilateral triangle
    printf("\n\nEnter 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\nEnter the width and height of the right angled triangle : ");
    scanf("%f%f",&w,&h);
    perimeter = w + h + sqrt(w*w + h*h);
    printf("Perimeter of right angled triangle is: %.3f",perimeter);

    //Perimeter of equilateral polygon
    printf("\n\nEnter the any side of the equilateral polygon : ");
    scanf("%f",&a);
    printf("Enter the total numbers of sides of equilateral polygon : ");
    scanf("%d",&n);
    perimeter = n * a;
    printf("Perimeter of equilateral polygon is: %.3f",perimeter);

    //Perimeter regular polygon
    printf("\n\nEnter the total number of sides in the regular polygon : ");
    scanf("%d",&n);
    printf("Enter the distance between any vertex and center of the regular polygon : ");
    scanf("%f",&a);
    perimeter = 2 * n * a * sin(M_PI/n);
    printf("Perimeter of regular polygon is: %.3f",perimeter);

    //Perimeter of general polygon
    printf("\n\nEnter size of sides of the general polygon. To exit enter zero : ");
    perimeter = 0;
    while(1){
         scanf("%f",&a);
         if(a < 1)
             break;
         else
             perimeter = perimeter + a;
    }
    printf("Perimeter of general polygon is: %.3f",perimeter);

    return 0;
}


Sample output:

Enter the size of any side square: 4
Perimeter of square is: 16.000

Enter width and length of the rectangle: 4 8
Perimeter of rectangle is: 24.000

Enter the size of all sides of the triangle: 3 4 5
Perimeter of triangle is: 12.000

Enter the radius of the circle: 4
Perimeter of circle is: 25.133

Enter width, length and height of the cuboids: 5 10 15
Perimeter of cuboids is: 120.000

Enter any side of the cube: 4
Perimeter of cube is: 48.000

Enter any side of the rhombus: 4
Perimeter of rhombus is: 16.000

Enter size of any two consecutive side of parallelogram: 4 8
Perimeter of parallelogram is: 24.000

Enter any side of the equilateral triangle: 3
Perimeter of equilateral triangle is: 9.000

Enter the width and height of the right angled triangle: 5 12
Perimeter of right angled triangle is: 30.000

Enter the any side of the equilateral polygon: 5
Enter the total numbers of sides of equilateral polygon: 8
Perimeter of equilateral polygon is: 40.000

Enter the total number of sides in the regular polygon: 6
Enter the distance between any vertex and center of the regular polygon: 11
Perimeter of regular polygon is: 66.000

Enter size of sides of the general polygon. To exit enter zero: 5 7 2 9 0
Perimeter of general polygon is: 23.000




1 comment:

Finding the Area of a Circle said...

Nice blog where i got the whole list of finding areas including Finding the Area of a Circle which i was searching .Thanks .