Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to calculate the area of a circle

C++ Code Example — Basic Programs

ADVERTISEMENT

WAP to calculate the area of a circle

Objective

Write a C++ program to calculate the area of a circle.

Algorithm / Approach

  1. Declare an integer r for the radius and a double area.
  2. Read the radius from the user.
  3. Calculate the area using the mathematical formula (Pi * r^2): area = 3.14 * r * r.
  4. Print the resulting area.
main.cpp
#include<iostream>
using namespace std;
int main() {
 int r;
 double area;
 cout<<"Enter Radius: ";
 cin>>r;
 area = 3.14*r*r;
 cout<<"Area = "<< area<< endl;
 return 0;
}

Expected Output

Enter Radius: 7
Area = 153.86

Explanation of the Program

  • Because multiplying by a decimal (3.14) can result in fractional numbers, the area variable must be declared as a floating-point type (like double or float).
  • If you declared area as an int, the decimal portion of the result would be truncated and permanently lost.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT