Program 54 - CSU1128 - CSE 2026 - Shoolini University

Program to calculate the value of Sin(x) series where x entered by the user. Sin series is sin(x): x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ....

Explanation

The program calculates the value of the sine function \( \sin(x) \) using the Taylor series expansion up to a specified number of terms. The sine series is given by \( \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \frac{x^9}{9!} - \ldots \), where \( x \) is entered by the user in degrees.

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 fact to calculate the factorial of a number:

  • long long fact(int n): Declares the function with an integer parameter n.

Function Definition

The program defines the fact function:

  • if (n == 0) return 1;: Defines the base case where the factorial of 0 is 1.
  • return n * fact(n - 1);: Defines the recursive case where the factorial of n is equal to n multiplied by the factorial of n - 1.

Variable Declaration

The main() function is defined, and several variables are declared:

  • float x, Q, sum = 0;: Variables to hold the input value \( x \), its radian conversion \( Q \), and the sum of the series.
  • int i, j, limit;: Variables to control the loop and the limit for the series expansion.

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 limit for the series expansion:

  • printf("Enter the value of x of sinx series (in degrees): ");: Prompts the user to enter the value of \( x \) in degrees.
  • scanf("%f", &x);: Reads the value entered by the user and stores it in x.
  • printf("Enter the limit upto which you want to expand the series: ");: Prompts the user to enter the limit for the series expansion.
  • scanf("%d", &limit);: Reads the limit entered by the user and stores it in limit.

Angle Conversion

The program converts the angle from degrees to radians:

  • Q = x;: Stores the original value of \( x \) for output.
  • x = x * (3.1415 / 180);: Converts the angle from degrees to radians.

Series Calculation

The program calculates the value of the sine function using the series expansion:

  • for (i = 1, j = 1; i <= limit; i++, j = j + 2): Loops through the terms of the series.
  • if (i % 2 != 0): Checks if the term index is odd.
    • sum += pow(x, j) / fact(j);: Adds the current term to the sum if the term index is odd.
  • else: Executes if the term index is even.
    • sum -= pow(x, j) / fact(j);: Subtracts the current term from the sum if the term index is even.

Output Result

The program prints the calculated value of the sine function:

  • printf("Sin(%.1f degrees): %f\n\n", Q, sum);: Prints the value of \( x \) in degrees and the calculated sum.

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>

long long fact(int n) {
    if (n == 0) return 1;
    return n * fact(n - 1);
}

int main() {
    printf("\n\n Program to calculate the value of Sin(x) series where x entered by the user. Sin series is sin(x): x - x^3/3! + x^5/5! - x^7/7! + x^9/9! .... \n\n");

    float x, Q, sum = 0;
    int i, j, limit;

    printf("Enter the value of x of sinx series (in degrees): ");
    scanf("%f", &x);

    printf("Enter the limit upto which you want to expand the series: ");
    scanf("%d", &limit);

    Q = x;
    x = x * (3.1415 / 180);  // Convert degree to radian

    for (i = 1, j = 1; i <= limit; i++, j = j + 2) {
        if (i % 2 != 0) {
            sum += pow(x, j) / fact(j);
        } else {
            sum -= pow(x, j) / fact(j);
        }
    }

    printf("Sin(%.1f degrees): %f\n\n", Q, sum);
    return 0;
}
                

Output

Program to calculate the value of Sin(x) series where x entered by the user. Sin series is sin(x): x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ....

Enter the value of x of sinx series: 4
Enter the limit upto which you want to expand the series: 5
Sin(4.0): 0.069754