PPS Practical 45

PPS Practical 45
  • Aim: Write a C program to use recursive calls to evaluate F(x) = x - x^3/ 3! + x^5/ 5 ! - x^7/ 7! +… x^n/ n!.
  • Code:
    //For turbo C++ remove below comments
    #include <stdio.h>
    //#include<conio.h>
    #include<math.h>
        int fact(int n){
        if (n==1){
            return 1;
        }
        return n*fact(n-1);
    }
    float series(int x, int n){
        static float sum;
        if (n==1){
            return sum+=x;
        }
        if (n%2==0){
            sum-=(pow(x,(2*n)-1)/fact((2*n)-1));
        }else{
            sum+=(pow(x,(2*n)-1)/fact((2*n)-1));
        }
        return series(x,--n);
    }
    void main()
    {
        int x,n;
        //clrscr();
        printf("for equation : x - x^3/ 3! + x^5/ 5 ! - x^7/ 7! +...+ x^n/ n! \nEnter value of x : ");
        scanf("%d",&x);
        printf("Enter value of n : ");
        scanf("%d",&n);
        printf("Sum : %f",series(x,n));
        // getch();
    }
  • Output:
    Output of practical 45

Comments