In: Computer Science
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π
Sample:
Enter the radius of your circle (-1 to end): 1
Area of circle with radius 1 is 3.14159
Enter the radius of your circle (-1 to end): 2
Area of circle with radius 2 is 12.5664
Enter the radius of your circle (-1 to end): -1
Good bye!
C++ code:
#include <iostream>
#include <cmath>
using namespace std;
//defining inline function circleArea
inline float circleArea(int r){
//returning Area
return M_PI*r*r;
}
int main()
{
//initializing radius r
float r;
//asking for radius
cout<<"Enter the radius of your circle (-1 to end): ";
//accepting it
cin>>r;
//looping till -1 is entered
while(r!=-1){
//printing Area
cout<<"Area of circle with radius "<<r<<" is
"<<circleArea(r)<<endl;
//asking for radius
cout<<"Enter the radius of your circle (-1 to end): ";
//accepting it
cin>>r;
}
//printing Good bye
cout<<"Good bye!"<<endl;
return 0;
}
Screenshot:
Input and Output: