[try Beta version]
Not logged in

 
Decimal value

Sep 22, 2013 at 8:29am
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<math.h>
using namespace 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;
}
Sep 22, 2013 at 8:32am
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.
Last edited on Sep 22, 2013 at 8:33am
Sep 22, 2013 at 9:38am
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 radius2.
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.
Sep 22, 2013 at 10:00am
Change the Circumference from int to float

so, it should be like this:

1
2
float Circumference;
float Area;
Last edited on Sep 22, 2013 at 10:01am
Sep 22, 2013 at 10:51am
Chervil did a better one. Double gives more accuracy and points of prescision.
Topic archived. No new replies allowed.