9. C PROGRAMMING LAB | Check Now

C PROGRAMMING LAB –09] Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result with the built-in Library function. Print both the results with appropriate messages.


Algorithm

  • Step-1: Start
  • Step-2: [Read the value of x in degree]- Read x
  • Step-3: [Initialization and Radians Conversion]
    • Temp = x
    • x=x*(3.142/180.0)
    • Term = x
    • sinx = term
    • n=1
  • Step-4: [Computer sin value]
    • Repeat while (term>FLT_EPSILON)
    • Fact = 2*n*(2*n+1)
    • Term = term * x * x /fact Sinx
    • sinx +term = n + 1n
  • Step-5: [Output]- Print sin(x) without using library function of Print sin(x) and with using library function
  • Step-6: Stop
See also  4. C PROGRAMMING LAB | Check Now

C PROGRAMMING -Flow Chart

C PROGRAMMING-compute Sin(x) using Taylor series approximation

Program -9 source code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void  main()
{
 int x,n,i;
 float rad, res, sum=0;
 printf("Enter degree\n");
 scanf("%d",&x);
 printf("Enter number of terms\n");
 scanf("%d",&n);

 rad=x*3.14/180;
 for(i=1;i<=n;i+=2)
 {
  if ((i-1)%4==0)
      sum=sum+pow(rad,i)/fact(i);
  else
      sum=sum-pow(rad,i)/fact(i); 
 }
 printf("Calculate sin(%d) = %f", x,sum);
 printf("\nLibrary sin(%d) = %f", x,sin(rad));
}

int fact(int m)
{
 int i,f=1;
 for(i=1;i<=m;i++)
 {
    f=f*i;
 }
 return 1;

}

Output

Enter degree

30

Enter the number of terms

1

Calculate sin(30) = 0.523333

Library sin(30) = 0.499770


C PROGRAMMING -Viva Questions

1] What is pre-processor directive?

2] What is the difference between const and #define?

3] What is the use of fabs()

4] What is variable initialization and why is it so important?

5] What is the difference between the = symbol and == symbol?

See also  7. AI AND MACHINE LEARNING VTU LAB | READ NOW

6] Can the curly bracket { } be used to enclose a single line of code?

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts