Followers

Friday, April 7, 2023

Program To Calculate The Area Of A Circle Using The Radius (Input By the User) Using Function

Program:
 #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.

1 comment:

  1. The post above provides you the code with the explanation.

    ReplyDelete

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...