Followers

Monday, April 10, 2023

Program To Calculate The Volume Of The Sphere Using Function

Program:
 #include <iostream>   
 using namespace std;   
 double calcSphereVol(double r){   // r is the radius of the sphere entered by the user
      return 4/3 * (3.14 * (r * r * r));  
 }   
  int main(){   
   double radius;  
      cout<<"Enter the radius of the sphere: ";  
      cin>>radius;  
      cout<<"The volume of sphere of radius "<<radius<<" is "<<calcSphereVol(radius);  
    return 0;   
  }   
Output: 






Working Of The Program:

1. The working of the program is explained as follows:

2. The program starts by including the necessary header files for input/output operations.
3. The calcSphereVol function takes a double r as input, which represents the radius of a sphere, and calculates the volume of the sphere using the formula 4/3 * (3.14 * (r * r * r)).
4. The main function starts by declaring a double variable radius.
5. It then prompts the user to enter the radius of the sphere using cout and cin statements.
6. The user input is stored in the radius variable.
7. The main function then calls the calcSphereVol function, passing the radius as an argument.
8. The calculated volume of the sphere is printed to the console using cout statement.
9. Finally, the main function returns 0, indicating the successful execution of the pro
gram.

No comments:

Post a Comment

Program to Find the Length of a String

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