Quiz #3

--Originally published at my programming blog

Today I did the Quiz #3, at first I was lost I didn’t understand very well how to make a function, but I asked for help and my classmates and my teacher helped me.

Once you understand it it’s not that hard.

What I needed to do was ask the user for a number and the calculate the square and cube root of that number, also I needed to include what happens if the user enters a negative number.

First I included the library cmath so I could use math functions in my program, after that I created my two functions, one for the square root and the other for the cube root and I specified what the function needed to do. And then started with the ” int main ()” and all my basic program and at the end I added an “if” to give the option of when the user entered a negative number.

So this is my code:

screen-shot-2017-01-26-at-10-15-19-am

#include
#include
using namespace std;

double square_root(double x){
return pow (x, 0.5);
}
double cube_root(double x){
return pow (x, 0.333);
}

int main(){
double x;
cout<<“Let’s find the square and cube root of a number!”<<endl;
cout<< “Give me a number:”<<endl; cin>>x;
if (x<0){
cout<<“Error! The number you entered is negative.”<<endl;
} else {
cout<< “The square root is: “<<square_root(x)<<endl;
cout<< “The cube root is: “<<cube_root(x)<<endl;
return 0;
}
}

And this is the program when I run it:

screen-shot-2017-01-26-at-10-20-35-amscreen-shot-2017-01-26-at-10-19-59-am