### Area in C++

I am trying to write a program that calculates the area of a circle with an entered diameter and calculate the are of a square and an equilateral triangle with the length of the diameter. Is this correct?

#include <iostream>
using namespace std;

#include <math.h>
int main()
{

int diameter_of_circle;
int area_of_circle;
int area_of_square;
int area_of_equilateral_triangle;
double square_of_diameter = diameter_of_circle *diameter_of_circle;

cout<< "What is the diameter of the circle? ";
cin >> diameter_of_circle ;

cout << "The area of the circle is " << area_of_circle <<'\n';

area_of_square = square_of_diameter;
cout <<"The area of a square is " << area_of_square << '\n';

area_of_equilateral_triangle = square_of_diameter * pow(3, 0.5) /4;

cout << "The area of the equilateral triangle is " <<area_of_equilateral_triangle<< endl;

return 0;

}
Last edited on
You have a number of problems.
1) diameter, area, radius are all integers.
2) Your value of pi is not very precise.
3) The calculation of area_of_circle will get truncated to an integer.
4) square_of_diameter is calculated BEFORE you prompt for diameter_of_circle.
5) area_of_equilateral_triangle is also invalid because it uses square_of_diameter which was calculated from an uninitialized value.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

Is this correct?

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include using namespace std; #include int main() { int diameter_of_circle; int area_of_circle; int radius_of_circle; int area_of_square; int area_of_equilateral_triangle; cout<< "What is the diameter of the circle? "; cin >> diameter_of_circle ; int square_of_diameter = diameter_of_circle *diameter_of_circle; radius_of_circle = diameter_of_circle /2; area_of_circle = radius_of_circle *radius_of_circle * 3.1415926 ; cout << "The area of the circle is " << area_of_circle <<'\n'; area_of_square = square_of_diameter; cout <<"The area of a square is " << area_of_square << '\n'; area_of_equilateral_triangle = square_of_diameter * pow(3, 0.5) /4; cout << "The area of the equilateral triangle is " <
Last edited on
Should be divide by 2 on that triangle, right? (b*h)/2

I would assume that you are expected to ask for input three times, one for each shape.
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include using namespace std; #include int main() { cout.precision(18); double diameter_of_circle=0; double area_of_circle=0; double radius_of_circle=0; double area_of_square=0; double area_of_equilateral_triangle=0; cout<< "What is the diameter of the circle? "; cin >> diameter_of_circle ; double square_of_diameter = diameter_of_circle *diameter_of_circle; radius_of_circle = diameter_of_circle /2; area_of_circle = radius_of_circle *radius_of_circle * 3.141592653589793 ; cout << "The area of the circle is " << area_of_circle <<'\n'; area_of_square = square_of_diameter; cout <<"The area of a square is " << area_of_square << '\n'; area_of_equilateral_triangle = square_of_diameter * pow(3, 0.5) /4; cout << "The area of the equilateral triangle is " <

Use this code
Topic archived. No new replies allowed.