Skip to main content

Taylor series

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int ch,i;
    float num,den,x,degree,sum=0,term;
    printf("enter 1 for sin(x)\n enter 2 for cos(x)\n");
    scanf("%d",&ch);
    printf("enter the degree");
    x=(degree*3.14)/180.0;
    switch(ch)
    {
        case1:num=x;
        den=1;
        i=2;
        break;
        case2:num=1;
        den=1;
        i=1;
        break;
        default:printf("enter the proper choice\n");
        exit(0);
    }
    do
    {
        term=num/den;
        num=-num*x*x;
        den=den*i*(i+1);
        sum=sum+term;
        i+i+2;
    }
    while(fabs(term)>0.000001);
    if(ch==1)
    {
        printf("sin(%f) without built in function=%f\n",degree,sum);
        printf("sin(%f) with built in function=%f\n",degree,sin(x));
    }
    else
    {
          printf("cos(%f) without built in function=%f\n",degree,sum);
        printf("cos(%f) with built in function=%f\n",degree,cos(x));
    }
    return 0;
}

 Algorithm for Taylor series

Start

Read degree

x=degree*pi /180

Read choice 1: sin, 2: cos

If choice is 1: num = x, den = 1, i = 2

If choice is 2: num = 1, den = 1, i = 1

Do

term = num/den

sum = sum + term

i = i+2

num = -num * x * x

den = den * i * (i+1)

while(fabs(term) > 0.00001)

If choice is 1: print sum and sin(x)

If choice is 2: print sum and cos(x)

Stop

Comments

Popular posts from this blog

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are quarantined to prevent the spread of bird flu.

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are  quarantined  to prevent the spread of bird flu. immunized butchered secluded CORRECT mingled