Complementary task for topic: 1

M Nemeth · 2023-08-29 15:21:04.604221'

scanf: float II.

scanf: float II.

Task: Calculate the volume of a cylinder:
Write a C program that prompts the user to enter the radius and height of a cylinder using scanf(), and calculates its volume. Display the volume using printf().

Hint: In math.h there is a function for get any power:
pow()
so pow(2.0,3)→ gives 8.0
You need to include math.h!
Tip: you can use the multiplication multiple time, so 2.0*2.0*2.0→ 8.0 as well

Solution
#include 
#include 

int main() {
    float radius, height;
    const float pi = 3.14159;
    
    printf("Enter the radius of the cylinder: ");
    scanf("%f", &radius);
    
    printf("Enter the height of the cylinder: ");
    scanf("%f", &height);
    
    float volume = pi * pow(radius, 2) * height;
    
    printf("The volume of the cylinder is %.2f.\n", volume);
    
    return 0;
}



Explanation
In this example, the program uses printf() to prompt the user to enter the radius and height of a cylinder. The scanf() function is used to scan the input from the user and store it in the radius and height variables. The program calculates the volume of the cylinder using the formula pi * radius^2 * height, where pi is a constant with the value 3.14159. The volume is stored in the volume variable. Finally, it uses printf() to display the volume in the specified format.
< < previous    next > >