I need to calculate the area and circumference of a circle and the answer must be in decimals form. What should i change to get decimal values. Another thing is the line for Area formula gets error invalid operands of types double or int. How can i solve this problem? Thanks
#include<iostream>
#include<math.h>
usingnamespace std;
int main()
{
int radius;
int Area;
int Circumference;
Area = 2 * M_PI * radius^2;
Circumference = 2 * M_PI * radius;
cout << "Enter the value of radius \n";
cin >> radius;
cout << "The area of circle is" << Area << endl;
cout << "The circumference of circle is" << Circumference << endl;
return 0;
}
You sir/ma'am are using the wrong tool for the job. int specifies integers and last I checked integers didn't have a decimal in them. Your fix is just one google away with the keywords "float c++" in it.
Several issues.
Use type double rather than int for the variables.
The ^ operator is not used to find a number raised to a power. Instead, use radius*radius for radius^{2}.
If you want other powers, there is a function pow() but that's using a sledgehammer to crack a walnut here.
There is also an important logic error, you attempt to do all the calculations first, before getting the value of radius from the user. Make sure you do things in the correct order.