3. Write a C program that reads the value of angle x in radians (360 degrees = 2
ID: 672435 • Letter: 3
Question
3. Write a C program that reads the value of angle x in radians (360 degrees = 2 radians) then calls a function sin to find the sine trigonometric function by evaluating the following series for sin(x) to n terms (starting with x in the formula there, 4 terms have been shown, the others would simply follow the progression). The value of n would also be supplied by the user. sin(x) = x (x3 /3!) + (x5 /5!) (x7 / 7!) + ……. The output of your program should look something like the below text.
This program calculates the trigonometric sine for a given angle in radians! Enter the angle in radians: 0.5236 Enter the number of series terms that you would like to add up: 7 Value of sin(0.5236) = 0.5 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the angle in radians: 1.047 Enter the number of series terms that you would like to add up: 5 Value of sin(1.047) = .866 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye!
Explanation / Answer
CODE 1:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
int factorial(int a);
int n,b;
int lngFract[20];
int main(){
system("PAUSE");
int m,i,j;
float z,y,x,sinx;
printf("input a value for x :");
scanf("%f",&x);
printf("d= %f",x);
system("PAUSE");
b=1;
n=1;
j=3;
m=2;
i=1;
sinx=0;
while (i<5){
if (i == 1)
sinx = sinx + x;
else
{
z = pow(x,m + 1);
//printf("x= %f",z);
n = factorial(j);
//printf("n= %d",n);
y = z/n;
//printf("y= %f",y);
m = m + 2;
j = j + 2;
sinx = sinx + y;
//printf("sin%f=%f",x, sin(x));
}
++i;
}
printf("sin%f=%f",x, sin(x));
system("PAUSE");
return EXIT_SUCCESS;
//scanf_s("%f",&x); return 0;
}
int factorial(int a)
{
//printf("d= %d",n);
if (b==1)
{
n =a;
lngFract[b-1] = a;
b++;
}
else
{
lngFract[b-1] = a;
n = lngFract[b-1] * n;
//printf("d= %d",n);
b++;
}
if (a > 1)
{
factorial (a-1);
}
b=1;
return n;
}
CODE 2
#include<stdio.h>
#include<math.h>
void sine(float*);
float fact(float);
void sine(float *x)
{
float i,j=0;
float sum=0;
for(i=1;i<8;i=i+2)
{
sum=sum+pow(-1,j++)*(pow(*x,i)/fact(i));
}
printf("%f",sum);
}
float fact(float i)
{
float f=1;
while(i>=1)
{
f=f*i;
i--;
}
return f;
}
void main()
{
float x;
printf("Enter x ");
scanf("%f",&x);
sine(&x);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.