C Online Compiler
Example: Calculate Area of Circle Using User-Defined Function in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Calculate Area of Circle Using User-Defined Function #include <stdio.h> // Required for input/output operations // Define the constant PI for calculations const double PI = 3.14159; // Function to calculate the area of a circle // It takes a double 'radius' as input and returns the calculated area as a double. double calculateCircleArea(double radius) { // Step 1: Calculate the area using the formula: Area = PI * radius * radius double area = PI * radius * radius; // Step 2: Return the calculated area return area; } int main() { // Step 1: Declare a variable to store the radius entered by the user double radius; // Step 2: Declare a variable to store the calculated area double area; // Step 3: Prompt the user to enter the radius printf("Enter the radius of the circle: "); // Step 4: Read the radius from the user input // Use %lf for reading double values scanf("%lf", &radius); // Step 5: Call the user-defined function to calculate the area // Pass the 'radius' to the function and store the returned value in 'area' area = calculateCircleArea(radius); // Step 6: Display the calculated area to the user // Use %.2lf to print the double value with two decimal places printf("The area of the circle with radius %.2lf is: %.2lf\n", radius, area); return 0; // Indicate successful program execution }
Output
Clear
ADVERTISEMENTS