Simple circle calculator C++
Just a very simple but effective way to calculate a circle’s:
– Diameter
– Circumference
– Radius
– Area
Compile with G++ and run (only Linux)
$ g++ circle.cpp -o circle
$ ./circle
#include <iostream>
#include <math.h>
int main() {
const double pi=M_PI;
double radius, area, circumference, diameter;
char info;
system("clear");
std::cout<<"FYI, π="<<pi<<std::endl;
std::cout<<"What info do you have? (radius(r), diameter(d) or circumference(c): "<<std::endl;
std::cin>>info;
switch (info) {
case 'r':
std::cout<<"What is the radius? : "<<std::endl;
std::cin>>radius;
// Calculate circle
circumference=radius*2*pi;
diameter=circumference/pi;
area=radius*radius*pi;
// Show results
std::cout<<"\n\033[1;32mCircle information: \033[0m"<<std::endl;
std::cout<<"Radius: "<<radius<<std::endl;
std::cout<<"Diameter: "<<diameter<<std::endl;
std::cout<<"Circumference: "<<circumference<<std::endl;
std::cout<<"Area: "<<area<<std::endl;
break;
case 'd':
std::cout<<"What is the diameter? : "<<std::endl;
std::cin>>diameter;
// Calculate circle
radius=diameter/2;
circumference=radius*2*pi;
area=radius*radius*pi;
// Show results
std::cout<<"\n\033[1;32mCircle information: \033[0m"<<std::endl;
std::cout<<"Radius: "<<radius<<std::endl;
std::cout<<"Diameter: "<<diameter<<std::endl;
std::cout<<"Circumference: "<<circumference<<std::endl;
std::cout<<"Area: "<<area<<std::endl;
break;
case 'c':
std::cout<<"What is the circumference? : "<<std::endl;
std::cin>>circumference;
// Calculate circle
diameter=circumference/pi;
radius=diameter/2;
area=radius*radius*pi;
// Show results
std::cout<<"\n\033[1;32mCircle information: \033[0m"<<std::endl;
std::cout<<"Radius: "<<radius<<std::endl;
std::cout<<"Diameter: "<<diameter<<std::endl;
std::cout<<"Circumference: "<<circumference<<std::endl;
std::cout<<"Area: "<<area<<std::endl;
break;
default:
std::cout<<"\033[1;31mNo valid operator given...\033[0m\n"<<std::endl;
}
return 0;
}