Calculate the Area of a Circle in C++

This exercise helps you practice using variables, mathematical operations, and user input/output in C++ by calculating the area of a circle. The formula to compute the area of a circle is A = π * r², where π (pi) is approximately 3.14159 and r is the radius of the circle.

By completing this program, you'll reinforce how to work with floating-point numbers, constants, and the standard input/output stream. This is an essential skill when building applications that involve geometry, simulations, or any kind of measurement-based calculations.

Key Concepts:
- Declaring floating-point variables
- Using mathematical expressions
- Reading user input with `cin`
- Displaying results with `cout`

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

 Copy 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

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Multiplication Table Generator in C++

    This exercise focuses on generating the multiplication table of a number provided by the user. It introduces the use of loops in C++, specifically the `for` loop, to repeat a set o...

  • Check if a Number is Even or Odd in C++

    This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand ...

  • Currency Converter: Euros to US Dollars in C++

    This exercise is designed to help you understand how to work with floating-point numbers and user input/output in C++. You will build a basic currency converter that takes an amoun...

  • Calculate the Perimeter of a Rectangle in C++

    This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output,...

  • Sum of the First N Natural Numbers in C++

    This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the ...

  • Print the First 10 Fibonacci Numbers in C++

    This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that p...