Complementary task for topic: 4

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

Functions: Calculate Circle Area

Functions: Calculate Circle Area

Write a C function circle_area that takes the radius of a circle (a floating-point number) as input and returns the area of the circle as a floating-point number. Use the formula: Area = PI* radius * radius. Take PI as 3.14159.

Hint:

Solution
#include 

// Function to calculate the area of a circle
float circle_area(float radius) {
    const float PI = 3.14159;
    return PI * radius * radius;
}

int main() {
    float radius;
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    float area = circle_area(radius);
    printf("The area of the circle with radius %.2f is %.2f.\n", radius, area);

    return 0;
}



Explanation
The circle_area function takes the radius of a circle as input and returns the area of the circle using the formula: Area = PI * radius * radius. The value of PI is defined as 3.14159 (you can use the built-in M_PI constant from math.h instead). In the main function, the user is asked to input the radius, and the circle_area function is called to calculate and print the area of the circle.
< < previous    next > >