Hello all,
I'm new to programming and I came across the practice problems thread and tried one of the beginner ones:
"Make a program that allows the user to input either the radius, diameter, or area of the circle. The program should then calculate the other 2 based on the input."
I tried it out and it seems to work.
Is there any way this can be improved or made more efficient and is there any unneeded code?
Thanks for your help.
// find radius, diameter, or area using one of the three
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
void displayInstructions()
{
cout << "Enter one of the following for a circle:\n"
<< "Radius, diameter, or area.\n"
<< "For unknowns, use 0" << endl;
}
void findCircleMeasurements()
{
double nRad, nDia, nArea; // set variables in circle
cout << "Enter Radius: ";
cin >> nRad;
cout << "\nEnter Diameter: ";
cin >> nDia;
cout << "\nEnter Area: ";
cin >> nArea;
cout << "\n";
if(nArea != 0)
{
nRad = sqrt(nArea/3.14); // radius == sqrt of area over pi
nDia= 2*nRad; // diameter == 2*radius
cout << "Radius is "
<< nRad
<< "\nDiameter is "
<< nDia;
return;
}
if(nRad != 0)
{
nDia = 2*nRad; // diameter == radius*2
nArea = pow(nRad,2)*3.14; // area == radius squared times pi
cout << "Diameter is "
<< nDia
<< "\nArea is "
<< nArea;
return;
}
if(nDia != 0)
{
nRad = nDia/2; // radius == diameter/2
nArea = pow(nRad,2)*3.14; // area == radius squared times pi
cout << "Radius is "
<< nRad;
cout << "/nArea is "
<< nArea;
return;
}
}
int main( int nNumberofArgs, char* pszArgs[])
{
displayInstructions(); // execute the two functions
findCircleMeasurements();
cin.get();
return 0;
}