Question

Here's the code.

int a;
int pi = 3.14;
int area;
int main()
{
    cout << "Input the radius of the circle ";
    cin >> a;

    a *= a *= pi >> area;

    cout << "The area is " << area;


}
Was it helpful?

Solution

The area of a circle is pi * r * r therefore you would want to do;

a = a * a * pi

Hope that helps

and they all would need to be floats.

OTHER TIPS

The >> operator when used with numbers is right shift, not assignment. You want something like

area = a * a * pi;

Update

You also need to use a floating point type or your answer won't be what you expect.

float a;
float pi = 3.14f;
float area;

I don't have enough patience to decipher your strange code. How about just area = a * a * pi?

Your code doesn't make any sense.

pi(and all your other variables) need to be double or float,... not int. An int can only contain an integral number. And pi is obviously not integral.

a *= a *= pi >> area; should be area = a * a * pi;

>> is a bitshift, not an assignment to the right side
*= is multiply assign and not just multiply. i.e. it is similar to left=left*right

Your code doesn't do what I think you wanted it to do. You don't assign to variables with >>; that is only for stream extraction (and bitshifting).

Also, a *= a *= pi probably doesn't do what you think it does.

Also, you want floating-point values, not int. An "int" pi is just 3.

Also, you should have error checking on your stream extraction!

Try:

int main()
{
    const float pi = 3.14;
    float a;

    cout << "Input the radius of the circle ";
    if (!(cin >> a)) {
         cout << "Invalid radius!";
         return 1;
    }

    float area = (a * a * pi);

    cout << "The area is " << area;
}
int pi = 3.14;

Wrong datatype. Assigning double value to int? That's wrong.

Write this:

double pi = 3.14;

And likewise, change other datatypes to double as well.

Because you're using int, or integer, for all your variables. You want to use doubles or even floats. (doubles are more precise).

All your variables are declared as int, which simply drops any fractional portion assigned to it. To work with floating-point values, use double instead.

Also, your equation in almost incomprehensible. Not sure what you're trying to do there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top