For this task , my classmates and I were instructed to create a program that asks the user for a number and then calls functions to calculate the square and cube roots of a number and prints them out.

For this quiz , I created the program with two functions:

  • double square_root(double x) {}  // returns the square root of x
  • double cube_root(double x) {} // returns the cube root of x

 

-Important notes:

  • I had to add a new library to my program , in order to use the square and cube roots functions:

#include <cmath>      // Declares a set of functions to compute common mathematical operations.

  • The progam does not accept negative numbers. It asks again for a positive number.

 

Here you will find my code and some captures:

quiz3-code
Atom code.

#include <iostream>
#include <cmath>      // Declares a set of functions to compute common mathematical operations.
using namespace std;

double x;

double square_root (double x) {
double square_root = sqrt (x);
return square_root;
}

double cube_root (double x) {
double cube_root = cbrt (x);
return cube_root;
}

int main(){
cout << “This program calculates the square and cube roots of a number.”<< endl;
cout << “Give me a number:”<< endl;
cin >> x;

if (x<=0){
cout << “Please , insert a positive number.”<< endl;
}else{
cout << “The square root of ” << x << ” is ” << sqrt (x) << endl;
cout << “The cube root of ” << x << ” is ” << cbrt (x) << endl;
}
return 0;
}

 

quiz3-cygwin
Running on Cygwin.

 

 

Until next time.

°Luis Felipe Garate Moreno.

Advertisement