C program to find the area of circle and cylinder using structures.

In C programming, we can calculate the area of a circle and the surface area and volume of a cylinder using structures to organize related data. A structure helps group related geometric properties like radius, height, and calculated areas in a single unit.

Syntax

struct shape {
    float radius;
    float height;
    float areacircle;
    float areacylinder;
    float volumecylinder;
};

Formulas Used

  • Area of circle: ? × radius²
  • Surface area of cylinder: 2? × radius × height + 2 × area of circle
  • Volume of cylinder: area of circle × height

Algorithm

Follow these steps to calculate geometric properties using structures −

  • Step 1: Define structure with required members
  • Step 2: Declare structure variable and initialize constants
  • Step 3: Input radius and height from user
  • Step 4: Calculate area of circle
  • Step 5: Calculate surface area of cylinder
  • Step 6: Calculate volume of cylinder

Example

The following program demonstrates how to use structures to calculate geometric properties −

#include <stdio.h>

struct shape {
    float radius;
    float height;
    float areacircle;
    float areacylinder;
    float volumecylinder;
};

int main() {
    struct shape s;
    float pi = 3.14159;
    
    /* Taking input from user */
    printf("Enter radius of cylinder: ");
    scanf("%f", &s.radius);
    printf("Enter height of cylinder: ");
    scanf("%f", &s.height);
    
    /* Calculate area of circle (base of cylinder) */
    s.areacircle = pi * s.radius * s.radius;
    printf("\nArea of circular cross-section: %.2f<br>", s.areacircle);
    
    /* Calculate surface area of cylinder */
    s.areacylinder = 2 * pi * s.radius * s.height + 2 * s.areacircle;
    printf("Surface area of cylinder: %.2f<br>", s.areacylinder);
    
    /* Calculate volume of cylinder */
    s.volumecylinder = s.areacircle * s.height;
    printf("Volume of cylinder: %.2f<br>", s.volumecylinder);
    
    return 0;
}
Enter radius of cylinder: 2
Enter height of cylinder: 5
Area of circular cross-section: 12.57
Surface area of cylinder: 87.96
Volume of cylinder: 62.83

Key Points

  • Structures help organize related geometric data in a single unit
  • The cylinder's surface area includes both circular bases and the curved surface
  • All calculations use the area of the circular base as a foundation
  • Using float data type provides decimal precision for geometric calculations

Conclusion

Using structures in C makes geometric calculations more organized and readable. This approach groups related properties together and demonstrates how structures can simplify complex mathematical computations involving multiple related values.

Updated on: 2026-03-15T14:09:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements