Using C++ Write a program that defines a macro called AREA to calculate the area
ID: 3847613 • Letter: U
Question
Using C++ Write a program that defines a macro called AREA to calculate the area of a circle ( PI times r times r ). The macro should have one argument, the radius. Use a preprocessor directive to define a symbolic constant PI to use in the macro. Assign a value to the radius in the main body of the program and calculate the area using the AREA macro, then output the area to the screen (you can reference the AREA macro in the cout statement if you wish). The screen output should resemble: The area of a circle with radius 3.75 is: 44.18
Explanation / Answer
#include <iostream>
#include <iomanip>
//macro defination for pi
#define PI 3.14159
//macro defination for area calculation
#define AREA(x) PI*x*x
using namespace std;
int main() {
double radius; //variable declared to store radius
cout << "Enter radius: ";
cin >> radius; //user input
double area = AREA(radius); // call to macro AREA and store results in a variable
cout << fixed;
cout << setprecision(2); //sets precision to 2 decimal places (since the example quoted by you shows 2 decimal places)
cout << "The area of a circle with radius " << radius << " is: " << area << endl; //prints area
return 0;
}
I have kept the code as simple as possible. I have also commented the code to make things easy. If incase you are still facing problem, please let me know, I shall be glad to help you with the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.