C Program to calculate the sum of first n terms of the Taylor series using function - IProgramX

Q. Write a function power, which calculates x^y . Write another function, which calculates n! Using for loop. Use these functions to calculate the sum of first n terms of the Taylor series
Taylor series: sin(x) = x - x^3/3!  + x^ 5 /5! 



Program

#include<stdio.h>
#include<math.h>
void cal();
void main()
{
cal();
}
void cal()
{
    int x,i;
    int fact = 1,n;
    float sum=0;

    printf("\nEnter the value of x in the series :  ");
    scanf("%d",&x);

    printf("\nEnter the number of terms in the series  :   ");
    scanf("%d",&n);

    for(i=1;i<n;i++)
    {
        fact = fact*i;
        sum = sum + (pow(x,i)/fact) ;

    }
    sum= sum +1;

    printf("\nThe sum of the taylor series is :  %.2f\n\n",sum);
}

Output:

Enter the value of x in the series :  4

Enter the number of terms in the series  :   6

The sum of the taylor series is :  42.87

Post a Comment

1 Comments

  1. def gcd(a, b):
    while b:
    a, b = b, a % b
    return a

    def display_state(jug1, jug2):
    print(f"{'JUG 1: ':>11}{jug1:<6} | {'JUG 2: ':>12}{jug2}")

    def pour_water(jug1, jug2, capacity1, capacity2):
    pour_amount = min(jug1, capacity2 - jug2)
    jug1 -= pour_amount
    jug2 += pour_amount
    return jug1, jug2

    def water_jug_solution(capacity1, capacity2, target_amount):
    jug1 = 0
    jug2 = 0
    print(f"Jug 1 Capacity: {capacity1} | Jug 2 Capacity: {capacity2}")

    while jug1 != target_amount and jug2 != target_amount:
    if jug1 == 0:
    jug1 = capacity1
    elif jug2 == capacity2:
    jug2 = 0
    else:
    jug1, jug2 = pour_water(jug1, jug2, capacity1, capacity2)
    display_state(jug1, jug2)

    first_jug = int(input("Enter the capacity of the first jug: "))
    second_jug = int(input("Enter the capacity of the second jug: "))
    target_amount = int(input("Enter the target liters of water: "))

    if target_amount < first_jug or target_amount < second_jug:
    if target_amount % gcd(first_jug, second_jug) == 0:
    water_jug_solution(first_jug, second_jug, target_amount)
    else:
    print("This is not possible....")
    else:
    print("This is not possible....")

    ReplyDelete