C Online Compiler
Example: Calculate Circle Area using Function in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Calculate Circle Area using Function #include <stdio.h> // Define PI as a constant for better precision and readability #define PI 3.14159265359 // Function declaration: Declares a function named calculateCircleArea // It takes one argument (radius of type double) and returns a double. double calculateCircleArea(double radius); int main() { double radius; // Variable to store the radius entered by the user double area; // Variable to store the calculated area // Step 1: Prompt the user to enter the radius of the circle printf("Enter the radius of the circle: "); // Step 2: Read the radius value from the user // %lf is used for reading a double scanf("%lf", &radius); // Step 3: Call the calculateCircleArea function // Pass the user-provided radius to the function // Store the returned area in the 'area' variable area = calculateCircleArea(radius); // Step 4: Display the calculated area to the user // %.2f formats the double to two decimal places printf("The area of the circle with radius %.2f is: %.2f\n", radius, area); return 0; // Indicate successful program execution } // Function definition: Implements the logic for calculateCircleArea // It takes 'r' (radius) as input double calculateCircleArea(double r) { // Calculate area using the formula A = PI * r * r return PI * r * r; }
Output
Clear
ADVERTISEMENTS