C Online Compiler
Example: Circle Area and Circumference Calculator in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Circle Area and Circumference Calculator #include <stdio.h> // Required for input/output operations (printf, scanf) #include <math.h> // Required for M_PI constant and mathematical functions // On some compilers, _USE_MATH_DEFINES is needed to expose M_PI #ifndef M_PI #define M_PI 3.14159265358979323846 #endif // Function to calculate the area of a circle // Takes the radius as a double and returns the area as a double double calculateArea(double radius) { return M_PI * radius * radius; } // Function to calculate the circumference of a circle // Takes the radius as a double and returns the circumference as a double double calculateCircumference(double radius) { return 2 * M_PI * radius; } int main() { // Step 1: Declare a variable to store the radius double radius; // Step 2: Prompt the user to enter the radius printf("Enter the radius of the circle: "); // Step 3: Read the radius from the user // %lf is used for reading double values with scanf scanf("%lf", &radius); // Step 4: Call the calculateArea function and store the result double area = calculateArea(radius); // Step 5: Call the calculateCircumference function and store the result double circumference = calculateCircumference(radius); // Step 6: Print the calculated area, formatted to two decimal places printf("Area of the circle: %.2f square units\n", area); // Step 7: Print the calculated circumference, formatted to two decimal places printf("Circumference of the circle: %.2f units\n", circumference); return 0; // Indicate successful program execution }
Output
Clear
ADVERTISEMENTS