/*
File: simpleMath.cpp
Name: Joshua T. Guerin
Purpose: Implement a few simple mathematical operations in C++.
Some of these should be familiar from class.
The rest should provide you with some interesting tricks.
*/
#include //Console input/output
using namespace std;
/*
int square(int num)
Purpose: Calculates the square of num
Return: the square of num
*/
int square(int num);
/*
int cube(int num)
Purpose: Calculates the cube of num
Return: the cube of num
*/
int cube(int num);
/*
int isEven(int num);
Purpose: Check number to determine if it is even or odd.
Return: true if num is even, false otherwise.
Note: Yup, you heard me right. You can even return a bool.
*/
bool isEven(int num);
int main() {
int number;
//Get a number from the user
cout << "Enter a number, please: ";
cin >> number;
//if the number is even, square it, otherwise cube it
if(isEven(number))
number = square(number);
else
number = cube(number);
//Print output to user.
cout << "If your number was even it has been squared "
<< "otherwise it has been cubed: "
<< number << endl;
cout << "Have a nice day : )" << endl << endl;
system("pause");
return 0;
}
int square(int num) {
int num_squared;
num_squared = num*num; //NOTE: num^2 will not work!
return num_squared;
}
int cube(int num) {
/*
See what I'm doing different between this and square()?
I'm not bothering to save the calculation because I don't need to.
I can simply return the result of the expression.
*/
return num * num * num;
}
//What do you think about this one?
bool isEven(int num) {
if(num%2 == 0) //num is even
return true;
//num is false
return false; //See why I don't need an else?
}