1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
/*write a program that asks the user to enter an integer x. Based on the value of x, do the following:
- if x is negative and even, calculate and display its value raised to the 3rd power
- if x is negative and odd, square it and display the result
- if x is greater than or equal to 0 and even, calculate and display the exponent of x, or exp(x)
- if x is grerter than or equal to 0 and odd, calculate and display the cube root of x
*/
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
//Declare variables
double x, y;
//Ask for Variable
cout << "Please enter a value for x: " << endl;
cin >> x;
if ((x < 0) && fmod(x , 2 == 0))
{
y = pow(x, 3);
cout << "y=" << y << endl;
} // If x is a negative number and divisable by 2, display the number cubed
else
{
if ((x < 0) && fmod(x, 2 == 0))
{
y = -pow(x, 2);
cout << "y=" << y << endl;
} // If x is a negative number but has a remanider when divided by 2, hence a odd number, display the number sqaured
else
{
if ((x > 0) && fmod(x, 2 == 0))
{
y = exp (x);
cout << "y=" << y << endl;
} // If x is a positive number and divasble by 2, display the exponent of that number
else
{
if ((x > 0) && fmod(x, 2 != 0))
{
y = cbrt (x);
cout << "y=" << y << endl;
} // If x is a postive number and has a remainder when divided by 2, hence an odd number, display the cube root
}
}
}
return 0;
}
|