Power and sin calculator where is used the “switch” condition

This C++ code give us the option to calculate the power of one number or to calculate the sin.It is good opportunity for “switch” condition use…Enjoy it!!!

****************************************************************************************************************************************************** #include <iostream>

using namespace std;

int main()
{

int choice=0;
cout << “1. power_calculation”<< endl ;
cout << “2. sin_calculation” << endl ;
cout << “Enter your choice [1, 2]: “;
cin >>choice;

switch(choice) {
case 1:
{
int base, exp;//variable for base and exponent
int result = 1;

cout << “Input the base: “;
cin >> base;
if (base<0)
{
cout << “Base must be positive number Try again: ” ;
}

else
cout << “Input the exponent: “;
cin >> exp;

for (int i=1; i<=exp; i++)//calculation of exponent
result= result*base; //calculation of result
cout << “The result is: ” << result;
break;
}
case 2:{
double o, h;
float sin;
cout << “Enter opposite length: ” << endl;
cin >> o;

cout << “Enter hypotenuse: ” << endl;
cin >> h;
sin = o/h;
cout << “Sine: ” << sin;
break;

default :
cout << “U have 2 choices” << endl;
}
}
return 0;
}

Advertisements