I am suppose to create a code that uses the pow function in math.h to calculate the hyperbolic cosine when the user inputs an angle x. the equation I have to use is cosh x= e^x + e^-x /2 can someone check my code. I am not sure what i am doing wrong. do I have to declare the constant e as a variable. I am totally lost!
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
float angleX; // Angle inputed by the user
//Statements
printf ("\nEnter angle in degrees:");
scanf ("%f", &angleX);
//Results
printf ("\nCalculation1 : (M_E^angleX + M_E^(-angleX))/2 = %f\n", pow(M_E,angleX)); /* calculates
the hyperbolic cosine using the pow function*/
return (0);
}//main
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
float angleX; // Angle inputed by the user
float coshx; // hyperbolic cosine of angle x
//Statements
printf ("\nEnter angle in degrees:");
scanf ("%f", &angleX);
//Calculations
coshx = (pow(M_E,angleX) + pow( M_E,-angleX))/2;//calculated the hyperbolic cosine using the pow function
//Results
printf ("\nCalculation1 : = %f\n", coshx);
return (0);
}//main
I changed some things and now it is atleast coming out with a calculation but it is the wrong answer
Hmm. Looks good to me. Can you produce the results for a few values of x?
Do you at least get cosh(0) = 1.0 ?
Your code is almost correct, but there are a few issues that need to be fixed. Here are some suggestions:
1. You need to include the desired equation for calculating the hyperbolic cosine using the `pow` function. Currently, you are only calculating `pow(M_E, angleX)`, which does not match the required equation. You need to incorporate the addition and division as well.
2. Since you want to use the constant `e` in the equation, you actually don't need to declare it as a variable. The constant `M_E` from the `math.h` library represents the mathematical constant `e`.
Here's an updated and corrected version of your code:
```C
#include <stdio.h>
#include <math.h>
int main(void)
{
// Local Declarations
float angleX; // Angle inputted by the user
// Statements
printf("Enter angle in degrees: ");
scanf("%f", &angleX);
// Calculation
float result = (pow(M_E, angleX) + pow(M_E, -angleX)) / 2.0;
// Results
printf("cosh(%f) = %f\n", angleX, result);
return 0;
}
```
In this updated code:
- The `result` variable is declared and used to calculate the hyperbolic cosine using the revised equation: `(pow(M_E, angleX) + pow(M_E, -angleX)) / 2.0`.
- The `printf` statement in the `main` function is updated to display the correct result with the input angle.
- The code now correctly calculates the hyperbolic cosine using the `pow` function and the constant `M_E`.
Please note that the `math.h` library should be included in your compiler settings to ensure that the `pow` function and the constant `M_E` are recognized and used correctly.