WAP to calculate the area of a circle
Objective
Write a C++ program to calculate the area of a circle.
Algorithm / Approach
- Declare an integer
rfor the radius and a doublearea. - Read the radius from the user.
- Calculate the area using the mathematical formula (Pi * r^2):
area = 3.14 * r * r. - 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
areavariable must be declared as a floating-point type (likedoubleorfloat). - If you declared
areaas anint, the decimal portion of the result would be truncated and permanently lost.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)