Home >Backend Development >C++ >C program written using structures to calculate the area of ​​circles and cylinders

C program written using structures to calculate the area of ​​circles and cylinders

PHPz
PHPzforward
2023-08-29 21:41:10966browse

C program written using structures to calculate the area of ​​circles and cylinders

In the C programming language, we can use structures to find the area of ​​a circle, the area and volume of a cylinder.

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

Refer to the algorithm given below to calculate the area and other parameters of circles and cylinders by using structures.

Step 1 - Declare the structure members.

Step 2 - Declare and initialize input variables.

Step 3 - Enter the length and radius of the cylinder.

Step 4 - Calculate the area of ​​the circle.

Step 5 - Calculate the area of ​​the cylinder.

Step 6 - Calculate the volume of the cylinder.

Example

The following is a C program that uses structures to calculate the area and other parameters of circles and cylinders-

Real-time demonstration

#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</p><p>",s.areacircle);
   //area of cylinder
   s.areacylinder = (float)2*pi*s.radius*s.line + 2 * s.areacircle;
   printf("Surface area of cylinder : %.2f</p><p>", s.areacylinder);
   //volume of cylinder
   s.volumecylinder = s.areacircle*s.line;
   printf("volume of cylinder : %.2f</p><p>", 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

The above is the detailed content of C program written using structures to calculate the area of ​​circles and cylinders. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete