#include <iostream>
using namespace std;
double calcArea(double radius){
double area = 3.14 * radius * radius; // area of circle = pi * r* r
return area;
}
int main(){
double radius;
cout<<"Enter the radius of the circle: ";
cin>>radius;
cout<<"The area of circle with radius "<<radius<<" is "<<calcArea(radius)<<endl;
return 0;
}
Output:
Working Of The Program:
This program calculates the area of a circle given the radius. Here is how it works:
1. The program defines a function called
calcArea that takes a single argument, radius, and returns the area of a circle with that radius.2. Inside the
calcArea function, the area is calculated using the formula area = pi * r * r, where pi is the mathematical constant pi (approximately equal to 3.14).3. The calculated area is then returned by the function using the
return statement.4. In the
main function, the user is prompted to enter the radius of the circle.5. The input radius is read from the user and stored in a double variable called
radius using the cin statement.6. The
calcArea function is called with the radius variable as an argument, and the returned area is printed to the console using the cout statement.
The post above provides you the code with the explanation.
ReplyDelete