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 radius_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 ;

radius_of_circle = diameter_of_circle /2;
area_of_circle = radius_of_circle *radius_of_circle *3.14;

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

#include <math.h> 
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 " <<area_of_equilateral_triangle<< endl;

	return 0;

}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

#include <math.h> 
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 " <<area_of_equilateral_triangle<< endl;

	return 0;

}


Use this code
Topic archived. No new replies allowed.