Answer to Question #337743 in C++ for Simon

Question #337743

Different shapes have different dimensions and properties that can be used to compute their area and perimeter. Using namespace technique, create a C++ program that compute the area of circle, rectangle, triangle and sphere. Each shape must have its own namespace that houses two functions, the one for calculating the area and the other for perimeter or circumference. 


1
Expert's answer
2022-05-05T16:26:15-0400
#include<iostream>
#include<math.h>


using namespace std;


namespace Circle{
	float radius;
	
	void AreaCircle(float radius){
		cout << "Area of Circle = " << M_PI*pow(radius,2) << endl;
	}
	void Circumference(float radius){
		cout << "Circumference = " << 2*M_PI*radius << endl;
	}
}
namespace Rectangle{
	float A, B;
	
	void AreaRectangle(float A, float B){
		cout << "Area of Rectangle = " << A*B << endl;
	}
	void PerimeterRectangle(float A, float B){
		cout << "Perimeter of Rectangle = " << 2*(A+B) << endl;
	}
}
namespace Triangle{
	float A, B, C;
	
	void AreaTriangle(float A, float B, float C){
		float p;
		p = (A+B+C)/2;
		cout << "Area of Triangle = " << sqrt(p*(p-A)*(p-B)*(p-C)) << endl;
	}
	void PerimeterTriangle(float A, float B, float C){
		cout << "Perimeter of Triangle = " << A+B+C << endl;
	}
}


namespace Sphere{
	float radius;
	
	void AreaSphere(float radius){
		cout << "Area of Sphere = " << 4*M_PI*pow(radius,2) << endl;
	}
	void Circumference(float radius){
		cout << "Circumference = " << 2*M_PI*radius << endl;
	}
}




int main()
{
	float a, b, c, s, radius, area;
	
	int choise;
	
	while (choise != 999){
		cout<<"\t1.Circle" << endl;
		cout<<"\t2.Rectangle" << endl;
		cout<<"\t3.Triangle" << endl;
		cout<<"\t4.Sphere" << endl;
		cout<<"\t5.Exit" << endl;
		
		cout<<"\n\tEnter Your Choice :" << endl;
		
		cin>>choise;
		
		switch(choise)
		{
			case 1:{
				cout << "Enter radius: ";
				cin >> Circle::radius;
				Circle::AreaCircle(Circle::radius);
				Circle::Circumference(Circle::radius);
				system("pause");
				break;
			}
			case 2:{
				cout << "Enter side A: ";
				cin >> Rectangle::A;
				cout << "Enter side B: ";
				cin >> Rectangle::B;
				Rectangle::AreaRectangle(Rectangle::A,Rectangle::B);
				Rectangle::PerimeterRectangle(Rectangle::A,Rectangle::B);
				system("pause");
				break;
			}
			case 3:{
				cout << "Enter side A: ";
				cin >> Triangle::A;
				cout << "Enter side B: ";
				cin >> Triangle::B;
				cout << "Enter side C: ";
				cin >> Triangle::C;
				Triangle::AreaTriangle(Triangle::A,Triangle::B,Triangle::C);
				Triangle::PerimeterTriangle(Triangle::A,Triangle::B,Triangle::C);
				system("pause");
				break;
			}
			case 4:{
				cout << "Enter radius: ";
				cin >> Sphere::radius;
				Sphere::AreaSphere(Sphere::radius);
				Sphere::Circumference(Sphere::radius);
				system("pause");
				break;
			}
			case 5:{
				char sure;
		 		cout << "You are sure? (y/n): ";
		 		cin >> sure;
		 		if (sure == 'y') choise = 999;
	   			else continue;
			}
			default: 
				cout<<"\n Invalid Choice Try Again...!!!" << endl;
				break;
		}
	}
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS