C Program to Calculate Area and Perimeter of Different Shapes

 

 

C Program to Calculate Area and Perimeter of Different Shapes

This page contains a C program that calculates the area and perimeter of different shapes (circle, rectangle, triangle). The program uses basic mathematical formulas to achieve this task.

Program Explanation

The C program to calculate the area and perimeter of different shapes follows these steps:

  1. Include necessary headers: Include the <stdio.h> header for input/output functions.
  2. Define Functions: Define separate functions to calculate the area and perimeter of each shape.
  3. Main Function: The main function is the entry point of the program.
  4. Input Parameters: Define the necessary input parameters for each shape.
  5. Call Functions: Call the respective functions to calculate the area and perimeter, and print the results.

C Program Code

/*
 * C program to calculate the area and perimeter of different shapes.
 */
#include <stdio.h>
#define PI 3.14159265358979323846

// Function prototypes
double circleArea(double radius);
double circlePerimeter(double radius);
double rectangleArea(double length, double width);
double rectanglePerimeter(double length, double width);
double triangleArea(double base, double height);
double trianglePerimeter(double a, double b, double c);

/**
 * Main function which is the entry point of the program.
 * @return 0 on successful execution
 */
int main() {
    // Circle parameters
    double radius = 5.0;
    printf("Circle: \n");
    printf("Area: %.2f\n", circleArea(radius));
    printf("Perimeter: %.2f\n\n", circlePerimeter(radius));

    // Rectangle parameters
    double length = 10.0;
    double width = 5.0;
    printf("Rectangle: \n");
    printf("Area: %.2f\n", rectangleArea(length, width));
    printf("Perimeter: %.2f\n\n", rectanglePerimeter(length, width));

    // Triangle parameters
    double a = 3.0;
    double b = 4.0;
    double c = 5.0;
    double base = 3.0;
    double height = 4.0;
    printf("Triangle: \n");
    printf("Area: %.2f\n", triangleArea(base, height));
    printf("Perimeter: %.2f\n", trianglePerimeter(a, b, c));

    return 0;
}

/**
 * Function to calculate the area of a circle.
 * @param radius the radius of the circle
 * @return the area of the circle
 */
double circleArea(double radius) {
    return PI * radius * radius;
}

/**
 * Function to calculate the perimeter of a circle.
 * @param radius the radius of the circle
 * @return the perimeter of the circle
 */
double circlePerimeter(double radius) {
    return 2 * PI * radius;
}

/**
 * Function to calculate the area of a rectangle.
 * @param length the length of the rectangle
 * @param width the width of the rectangle
 * @return the area of the rectangle
 */
double rectangleArea(double length, double width) {
    return length * width;
}

/**
 * Function to calculate the perimeter of a rectangle.
 * @param length the length of the rectangle
 * @param width the width of the rectangle
 * @return the perimeter of the rectangle
 */
double rectanglePerimeter(double length, double width) {
    return 2 * (length + width);
}

/**
 * Function to calculate the area of a triangle.
 * @param base the base of the triangle
 * @param height the height of the triangle
 * @return the area of the triangle
 */
double triangleArea(double base, double height) {
    return 0.5 * base * height;
}

/**
 * Function to calculate the perimeter of a triangle.
 * @param a side a of the triangle
 * @param b side b of the triangle
 * @param c side c of the triangle
 * @return the perimeter of the triangle
 */
double trianglePerimeter(double a, double b, double c) {
    return a + b + c;
}

Program Details

The program contains the following functions:

  • circleArea: Calculates the area of a circle given its radius.
  • circlePerimeter: Calculates the perimeter (circumference) of a circle given its radius.
  • rectangleArea: Calculates the area of a rectangle given its length and width.
  • rectanglePerimeter: Calculates the perimeter of a rectangle given its length and width.
  • triangleArea: Calculates the area of a triangle given its base and height.
  • trianglePerimeter: Calculates the perimeter of a triangle given the lengths of its three sides.

Running the Program

To run the program:

  1. Save the code in a file named shapes.c.
  2. Compile the program using the command: gcc shapes.c -o shapes -lm (the -lm flag links the math library).
  3. Run the compiled program using the command: ./shapes.

 

Explanation of the C Program:

  1. Header Inclusion: The program includes <stdio.h> for input/output functions.
  2. Function Prototypes: Function prototypes are declared for calculating the area and perimeter of circles, rectangles, and triangles.
  3. Main Function:
    • Defines parameters for the circle, rectangle, and triangle.
    • Calls respective functions to calculate the area and perimeter of each shape.
    • Prints the results to the console.
  4. Area and Perimeter Functions:
    • Circle:
      • circleArea(double radius): Returns the area using the formula πr².
      • circlePerimeter(double radius): Returns the perimeter using the formula 2πr.
    • Rectangle:
      • rectangleArea(double length, double width): Returns the area using the formula length × width.
      • rectanglePerimeter(double length, double width): Returns the perimeter using the formula 2(length + width).
    • Triangle:
      • triangleArea(double base, double height): Returns the area using the formula 0.5 × base × height.
      • trianglePerimeter(double a, double b, double c): Returns the perimeter by summing the lengths of the three sides.

One Reply to “C Program to Calculate Area and Perimeter of Different Shapes”

Leave a Reply to create backlinks for website free Cancel reply

Your email address will not be published. Required fields are marked *