C++ Online Compiler
Example: Circle Area and Circumference Calculator in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Circle Area and Circumference Calculator #include <iostream> // Required for input/output operations #include <cmath> // Required for M_PI if using it, though we'll define our own PI for clarity // Define PI as a constant for accurate calculations // Using a double for precision const double PI = 3.14159; // Function to calculate the area of a circle // Takes the radius as input and returns the area double calculateArea(double radius) { return PI * radius * radius; } // Function to calculate the circumference of a circle // Takes the radius as input and returns the circumference double calculateCircumference(double radius) { return 2 * PI * radius; } int main() { // Step 1: Declare a variable to store the radius double radius; // Step 2: Prompt the user to enter the radius std::cout << "Enter the radius of the circle: "; // Step 3: Read the radius from the user std::cin >> 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: Display the calculated area std::cout << "The area of the circle is: " << area << std::endl; // Step 7: Display the calculated circumference std::cout << "The circumference of the circle is: " << circumference << std::endl; return 0; // Indicate successful program execution }
Output
Clear
ADVERTISEMENTS