0% found this document useful (0 votes)
2 views

Introduction to C lab 6 and 7 programs library function Along with algorithms and flowcharts

The document provides lab programs for computing cosine and sine using Taylor series approximation in C programming. It includes algorithms, flowcharts, and example code for both functions, along with instructions for comparing results with built-in library functions. The programs prompt the user for the number of terms and angle in degrees, converting the angle to radians for calculations.

Uploaded by

angelotommy006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to C lab 6 and 7 programs library function Along with algorithms and flowcharts

The document provides lab programs for computing cosine and sine using Taylor series approximation in C programming. It includes algorithms, flowcharts, and example code for both functions, along with instructions for comparing results with built-in library functions. The programs prompt the user for the number of terms and angle in degrees, converting the angle to radians for calculations.

Uploaded by

angelotommy006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

INTRODUCTION TO C PROGRAMMING

LAB PROGRAMS
The following is the syntax of lab 6 and 7 programs given by professor Muralidharan sir

LAB PROGRAM 6
Compute cos(x) using Taylor series approximation. Compare you result
with the built-in library function. Print both the results with appropriate inferences.
#include <stdio.h>
#include <math.h>

int main()
{
int n, i;
float sum, x, dx, term;

printf("Input number of terms: ");


scanf("%d", &n);
printf("Input angle in degrees: ");
scanf("%f", &dx);

x = dx * (3.14 / 180.0);
sum = 1;
term = 1;

for (i = 1; i <= n; ++i)


{
term = -(x * x) / ((2 * i - 1) * (2 * i)) * term;
sum += term;
}

printf("Sum of series cos(%f) = %f\n", dx, sum);


printf("Sum from library cos(%f) = %f\n", dx, cos(x));

return 0;
}

Output:
Input number of terms: 10
Input angle in degrees: 60
Sum of series cos(60.000000) = 0.500460
Sum from library cos(60.000000) = 0.500460
LAB PROGRAM 7
Compute sin(x) using Taylor series approximation. Compare you result with the
built-in library function. Print both the results with appropriate inferences. Sine
function using Taylor Series.
#include <stdio.h>
#include <math.h>

int main()
{
int n, i;
float sum, x, dx, term;

printf("Input number of terms: ");


scanf("%d", &n);
printf("Input angle in degrees: ");
scanf("%f", &dx);

x = dx * (3.14 / 180.0);
sum = x;
term = x;

for (i = 1; i <= n; ++i)


{
term = -(x * x) / ((2 * i + 1) * (2 * i)) * term;
sum += term;
}

printf("Sum of series sin(%f) = %f\n", dx, sum);


printf("Sum from library sin(%f) = %f\n", dx, sin(x));

return 0;
}

Output:
Input number of terms: 10
Input angle in degrees: 30
Sum of series sin(30.000000) = 0.499770
Sum from library sin(30.000000) = 0.499770
Algorithm and Flowchart
LAB PROGRAM 6
Compute cos(x) using Taylor series approximation. Compare you result
with the built-in library function. Print both the results with appropriate inferences.
Algorithm
1. Start
2. Initialize Variables
• Declare integer n, float sum, x, dx, and term.
3. Input Number of Terms
• Prompt the user to input the number of terms (n) for the series.
4. Input Angle
• Prompt the user to input the angle in degrees (dx).
5. Convert Angle to Radians
• Convert dx from degrees to radians: x = dx * (3.14 / 180.0).
6. Initialize Sum and Term
• Set sum to 1 (the first term of the cosine series).
• Set term to 1 (the first term).
7. Loop from 1 to n
• For each i from 1 to n:
• Update term using the formula: term = -(x * x) / ((2 * i -
1) * (2 * i)) * term.
• Update sum by adding the current term to it.
8. Output Results
• Print the calculated sum of the series.
• Print the result from the standard library cos(x).
9. End

Flowchart Description
1. Start: Begin the program.
2. Input n: Read the number of terms.
3. Input dx: Read the angle in degrees.
4. Convert dx to Radians: Compute x = dx * (3.14 / 180.0).
5. Initialize Variables: Set sum = 1 and term = 1.
6. Loop Start: For i = 1 to n:
• Calculate term: Update term using the Taylor series formula.
• Update sum: Add the current term to sum.
7. Loop End: Once the loop is finished.
8. Output: Print the calculated sum and the value from cos(x).
9. End: Terminate the program.
Flowchart
• Start ➔ Input n ➔ Input dx ➔ Convert to Radians ➔ Initialize sum and term ➔
[Loop] (i from 1 to n)
• ➔ Calculate term ➔ Update sum ➔ [Loop back]
• ➔ Output sum ➔ Output cos(x) ➔ End

LAB PROGRAM 7
Compute sin(x) using Taylor series approximation. Compare you result with the
built-in library function. Print both the results with appropriate inferences. Sine
function using Taylor Series.
Algorithm
1. Start
2. Initialize Variables
• Declare integer n, float sum, x, dx, and term.
3. Input Number of Terms
• Prompt the user to input the number of terms (n) for the series.
4. Input Angle
• Prompt the user to input the angle in degrees (dx).
5. Convert Angle to Radians
• Convert dx from degrees to radians: x = dx * (3.14 / 180.0).
6. Initialize Sum and Term
• Set sum to x (the first term of the sine series).
• Set term to x (the first term).
7. Loop from 1 to n
• For each i from 1 to n:
• Update term using the formula: term = -(x * x) / ((2 * i +
1) * (2 * i)) * term.
• Update sum by adding the current term to it.
8. Output Results
• Print the calculated sum of the series.
• Print the result from the standard library sin(x).
9. End
Flowchart Description
1. Start: Begin the program.
2. Input n: Read the number of terms.
3. Input dx: Read the angle in degrees.
4. Convert dx to Radians: Compute x = dx * (3.14 / 180.0).
5. Initialize Variables: Set sum = x and term = x.
6. Loop Start: For i = 1 to n:
• Calculate term: Update term using the Taylor series formula.
• Update sum: Add the current term to sum.
7. Loop End: Once the loop is finished.
8. Output: Print the calculated sum and the value from sin(x).
9. End: Terminate the program.

Flowchart Visualization
You can visualize the flowchart as follows:
• Start ➔ Input n ➔ Input dx ➔ Convert to Radians ➔ Initialize sum and term ➔
[Loop] (i from 1 to n)
• ➔ Calculate term ➔ Update sum ➔ [Loop back]
• ➔ Output sum ➔ Output sin(x) ➔ End

You might also like