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

CServer Side ProgrammingProgramming

In C programming language, we can find the area of circle, area and volume of cylinder with the help of structures.

  • The logic used to find area of circle is as follows −
s.areacircle = (float)pi*s.radius*s.radius;
  • The logic used to find area of cylinder is as follows −
s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle;
  • The logic used to find the volume of cylinder is −
s.volumecylinder = s.areacircle*s.line;

Algorithm

Refer an algorithm given below to find the area of circle and cylinder along with other parameters by using structures.

Step 1 − Declare structure members.

Step 2 − Declare and initialize the input variables.

Step 3 − Enter length and radius of cylinder.

Step 4 − Compute area of circle.

Step 5 − Compute area of cylinder.

Step 6 − Compute volume of cylinder.

Example

Following is the C program to find the area of circle and cylinder along with other parameters by using structures −

 Live Demo

#include<stdio.h>
struct shape{
   float line;
   float radius;
   float areacircle;
   float areacylinder;
   float volumecylinder;
};
int main(){
   struct shape s;
   float pi = 3.14;
   //taking the input from user
   printf("Enter a length of line or height : ");
   scanf("%f",&s.line);
   printf("Enter a length of radius : ");
   scanf("%f",&s.radius);
   //area of circle
   s.areacircle = (float)pi*s.radius*s.radius;
   printf("Area of circular cross-section of cylinder : %.2f\n",s.areacircle);
   //area of cylinder
   s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle;
   printf("Surface area of cylinder : %.2f\n", s.areacylinder);
   //volume of cylinder
   s.volumecylinder = s.areacircle*s.line;
   printf("volume of cylinder : %.2f\n", s.volumecylinder);
   return 0;
}

Output

When the above program is executed, it produces the following output −

Enter a length of line or height: 34
Enter a length of radius: 2
Area of circular cross-section of cylinder: 12.56
Surface area of cylinder: 452.16
volume of cylinder : 427.04
raja
Published on 26-Mar-2021 07:53:53
Advertisements