Explanation
The program calculates the value of sine and cosine series for a given \( x \) and the number of terms \( n \) entered by the user. The sine and cosine series are expanded using their Taylor series representation.
Header
The code begins with including the standard input-output header file and math header file using #include <stdio.h>
and #include <math.h>
.
Function Declaration
The program declares a recursive function factorial
to compute the factorial of a number:
int factorial(int n)
: Declares the function with an integer parametern
.
Function Definition
The program defines the factorial
function:
if (n == 0) { return 1; }
: Defines the base case where the factorial of 0 is 1.return n * factorial(n - 1);
: Defines the recursive case where the factorial ofn
is equal ton
multiplied by the factorial ofn - 1
.
Variable Declaration
The main()
function is defined, and several variables are declared:
double x
: Variable to hold the input value \( x \) in radians.int n
: Variable to hold the number of terms for the series expansion.double sin_series = 0
: Variable to accumulate the sum of the sine series terms.double cos_series = 0
: Variable to accumulate the sum of the cosine series terms.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the value of \( x \) and the number of terms \( n \) for the series expansion:
printf("Enter a number for which you want to print its sine and cosine till its number of times series expansion should be done (Example: 23 4) - and press enter: ");
: Prompts the user to enter the value of \( x \) and \( n \).scanf("%lf %d", &x, &n);
: Reads the values entered by the user and stores them inx
andn
.
Series Calculation
The program calculates the sine and cosine series using the Taylor series expansion:
for (int i = 0; i <= n; i++)
: Loops through the terms of the series expansion.double a = pow(-1, i) * pow(x, 2 * i + 1) / factorial(2 * i + 1);
: Computes the coefficient for the current term of the sine series.sin_series += a;
: Adds the current term to the sine series.double b = pow(-1, i) * pow(x, 2 * i) / factorial(2 * i);
: Computes the coefficient for the current term of the cosine series.cos_series += b;
: Adds the current term to the cosine series.
Output Result
The program prints the calculated values of the sine and cosine series:
printf("Sine series: %f\n", sin_series);
: Prints the value of the sine series.printf("Cosine series: %f\n", cos_series);
: Prints the value of the cosine series.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
// Function to compute the factorial of a number
int factorial(int n)
{
if (n == 0)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
printf("\n\n Program to calculate the value of sine and cosine series where \n\tsin(x) = a₀ + a₁cos(x) + a₂cos(2x) + a₃cos(3x) + ... + ancos(nx) and\n \t cos(x) = b₀ + b₁cos(x) + b₂cos(2x) + b₃cos(3x) + ... + bncos(nx)\n x is entered by the user. \n\n");
// Define the value of x for which we want to compute the sine and cosine series
// double x = 1.5;
double x;
// Define the number of terms to use in the series expansion
// int n = 10;
int n;
printf("\n\n Program to get the sine and cosine series of the number till the times entered by the user. Example 10 1.5 = sine: 0.997467 and cosine: 0.070737 \n\n");
printf("Enter a number for which you want to print its sine and cosine till its number of times series expansion should be done(Example: 23 4) - and press enter: ");
scanf("%lf %d", &x, &n);
// Initialize the sine and cosine series
double sin_series = 0;
double cos_series = 0;
// Loop through the terms of the series expansion
for (int i = 0; i <= n; i++)
{
// Compute the coefficient for the current term of the sine series
double a = pow(-1, i) * pow(x, 2 * i + 1) / factorial(2 * i + 1);
// Add the current term to the sine series
sin_series += a;
// Compute the coefficient for the current term of the cosine series
double b = pow(-1, i) * pow(x, 2 * i) / factorial(2 * i);
// Add the current term to the cosine series
cos_series += b;
}
// Print the results
printf("Sine series: %f\n", sin_series);
printf("Cosine series: %f\n", cos_series);
return 0;
}
Output
Program to calculate the value of sine and cosine series where \n\tsin(x) = a₀ + a₁cos(x) + a₂cos(2x) + a₃cos(3x) + ... + ancos(nx) and\n \t cos(x) = b₀ + b₁cos(x) + b₂cos(2x) + b₃cos(3x) + ... + bncos(nx)\n x is entered by the user.
Program to get the sine and cosine series of the number till the times entered by the user. Example
10 1.5 = sine:
0.997467 and cosine: 0.070737
Enter a number for which you want to print its sine and cosine till its number of times series
expansion should be
done(Example: 23 4) - and press enter: 4 5
Sine series: -0.766805
Cosine series: -0.685785