Group
Introduction to C++
Objective
1. Declare a variable to store the radius of the circle.
2. Use a constant to represent the value of π (pi).
3. Ask the user to input the radius.
4. Use the formula area = π * radius * radius to calculate the area.
5. Display the calculated area using `cout`.
Calculate the area of a circle (π * radius^2).
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Main function - entry point of the program
int main() {
const double PI = 3.14159; // Define a constant for the value of pi
double radius, area; // Declare variables for the radius and the area of the circle
// Ask the user to enter the radius of the circle
cout << "Enter the radius of the circle: ";
cin >> radius; // Read the radius from user input
// Calculate the area using the formula: area = π * radius^2
area = PI * radius * radius;
// Display the calculated area
cout << "The area of the circle is: " << area << endl;
return 0; // Return 0 to indicate successful execution
}
Output
Enter the radius of the circle: 3
The area of the circle is: 28.2743