Q13 Write a C function to evaluate the sin(x) series.

Write a C function to evaluate the series

Program: 134

Write a C function to evaluate the sin(x) series.


(1) Using default sin() function
(2) Using functionality

#include<stdio.h>
#include<math.h>
int fact(int x);
void main()
{
    double x,ax,ss=0,sin_x=0, temp;
    int i,n,sign=-1;

    printf("Enter x: ");
    scanf("%lf", &x);
    printf("Enter n: ");
    scanf("%d", &n);
    //convert degree into radian
    ax=x;
    x = x*(3.14159/180);

    //functionality
    for(i=1;i<=n;i=i+2)
    {
        sign = -sign;
        temp = sign*pow(x,i)/fact(i);
        sin_x = temp + sin_x;
    }

    printf("Sin(%.2lf) Using Functionality: %lf", ax,sin_x);
    //check with sin function
    ss = sin(x);
    printf("\nSin(%.2lf) Using Math.h: %lf",ax,ss);
}
int fact(int n)
{
    int i,fact=1;
    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }

    return fact;

}

Output:

Enter x: 45
Enter n: 5
Sin(45.00) Using Functionality: 0.707143
Sin(45.00) Using Math.h: 0.707106

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.