C++ language using ChIDE or any platform that reads #include<stdio.h> and #inclu
ID: 3573868 • Letter: C
Question
C++ language using ChIDE or any platform that reads #include<stdio.h> and #include<math.h>
There are functions in the math.h library that include trigonometric functions (sine, cosine, tangent, etc.). Write a program that asks the user for an angle (in degrees). Then, ask the user to type a letter. If the user types a lower case letter, display the sine of the angle to four decimal places. If the user types an upper case letter, display the cosine of the angle to four decimal places.
Explanation / Answer
#include<iostream>
//include iomanip for set precission call
#include<iomanip>
//include math.h for sine ,cosine
#include<math.h>
//for islower and isupper functions
#include <ctype.h>
using namespace std;
int main()
{
//declare char for user to enter either upper or lower case charecter
char ch;
//declare variable to hold degree
double degree;
cout << "Enter an angle (in degrees): ";
cin >> degree;
cout << "*Enter any charecter, if lower letter entered program *calculates sine of an angle, if upper case letter entered program *calculates cosine of an angle " << endl;
cin >> ch;
//check letter entered by user is upper case,ASCII value for 'A' is 65 and 'Z' is 91
if (isupper(ch))
{
cout << "Sine of an angle " << " = " << setprecision(4) << sin(degree) << endl;
}
//check letter entered by user is lower case,ASCII value for 'a' is 97 and 'z' is 123
if (islower(ch))
{
cout << "Cosine of an angle " << " = " << setprecision(4) << cos(degree) << endl;
}
}
---------------------------------------------------------------------------
output:
Enter an angle (in degrees): 45
*Enter any charecter, if lower letter entered program
*calculates sine of an angle, if upper case letter entered program
*calculates cosine of an angle
Z
Sine of an angle = 0.8509
---------------------------------
Enter an angle (in degrees): 120
*Enter any charecter, if lower letter entered program
*calculates sine of an angle, if upper case letter entered program
*calculates cosine of an angle
c
Cosine of an angle = 0.8142
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.