A sensor in a robotic arm needs to calculate the angle of rotation in real-time, but the
hardware doesn't support built-in trigonometric functions. Develop a C program to
approximate the value of sin(x) using a series expansion method for improved
performance.
Aim: To develop a C program to approximate the value of sin(x) using a series expansion
method for improved performance
Algorithm
Step 1: Start
Step 2: Read angle.
Step 3: Read number of terms n.
Step 4: Initialize
term = x
sum = x
i=1
Step 5: Repeat until i < n
Update term = (-term * x * x) / ((2 * i) * (2 * i + 1))
Update sum = sum + term
Increment i = i + 1
Step 6: Display sum as the approximated sin value.
Step 7: Stop
Program:
#include <stdio.h>
#include <math.h>
#define PI 3.14
#include<conio.h>
int main() {
int n, i;
float x, term, sum, degree;
clrscr();
printf("Enter the angle in degree: ");
scanf("%f", °ree);
x= (degree * PI)/180.0;
printf("Enter number of terms: ");
scanf("%d", &n);
term = x; // first term
sum = x;
for (i = 1; i < n; i++)
term = (-term * x * x) / ((2 * i) * (2 * i + 1));
sum += term;
printf("Approximated sin(%.2f) = %.6f\n", x, sum);
getch();
return 0;
Flowchart:
Result:
The program to approximate the value of sin(x) using a series expansion method for
improved performance has been executed successfully and the output was verified.