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
Release: 2023-08-29 21:41:10
forward
997 people have browsed it

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;
Copy after login
  • 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;
    Copy after login
    The logic used to find the volume of the
  • cylinder is −
  • s.volumecylinder = s.areacircle*s.line;
    Copy after login
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;
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template