Menu

Wednesday, 13 May 2015

Chapter 5,Problem f: Let Us C

Problem Statement:

Write a C function to evaluate the series 
sin(x) = x − (x 3 / 3!) + (x 5 / 5!) − (x 7 / 7!) +Lto five significant digits.

Solution:

//Ahmad Furqan
//P5.f
#include <iostream>
#include <conio.h>
using namespace std;
int fact(int num)
{
int f = num;
for (int i = num-1; i > 0; i--)
f = f*i;
return f;
}
int pow(int a, int b)
{
int p = a;
for (int i = 1; i < b; i--)
p *= a;
return p;
}
float sin_ser(float ang)
{
float s;
s = ang - (pow(ang, 3) / fact(3)) + (pow(ang, 5) / fact(5)) - (pow(ang, 7) / fact

(7))+(pow(ang,9)/fact(9));
return s;
}
void main(void)
{
float ang;
cout << "Enter an angle: ";
cin >> ang;
cout << "sin(" << ang << ")= " << sin_ser(ang);
_getch();
}

No comments:

Post a Comment