Advertisement
Felanpro

Basic Program (Knowledge Needed)

Nov 3rd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. const double PI = 3.14159; //Const double means that 3.14159 is const double PI. So just type PI or (const)PI in any function to make it work.
  8.  
  9. int getPositiveInt(string message)
  10. {
  11.     int num = 0;
  12.    
  13.     do
  14.     {
  15.     cout << message << endl;
  16.     cin >> num;
  17.     }while(num <= 0);
  18.    
  19.     return num;
  20. }
  21.  
  22. double areaOfCircle(int r)
  23. {
  24.        return PI * powf(r, 2);             //powf() is a function and for it to work you need to add the #include <cmath>
  25. }                                          //But pow() could work if you change add #include <math.h>
  26.  
  27. double volOfSphere(int r)
  28. {
  29.        return 4/3.0 * PI * powf(r, 3);      
  30. }
  31.  
  32. int main()
  33. {
  34.        int radius = getPositiveInt("Enter a positive integer for the radius of a circle/sphere: ");  
  35.        
  36.        double aCircle = areaOfCircle(radius);
  37.        
  38.        double vSphere = volOfSphere(radius);
  39.        
  40.        cout << "The area of a circle that has a radius of " <<
  41.        radius << " is " << aCircle << "." << endl;
  42.        
  43.        cout << "The volume of a sphere that has a radius of " <<
  44.        radius << " is " << vSphere << "." << endl;
  45.        
  46.        system("pause");
  47.        
  48.        return 0;      
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement