Friday, November 23, 2012

WRITE A C PROGRAM TO SUM THE SERIES:


/* WRITE A C PROGRAM TO SUM THE SERIES:

                                                   x       x^2     x^3
                                e^x = 1 + ----- + ----- + ----- + ...
                                                   1!        2!       3!          */


#include<stdio.h>
#include<conio.h>
#include<math.h>

#define PI 3.143

long int fact(int n)  /* To find factorial */
{
            if(n==0)
                        return 1;
            else
                        return(n*fact(n-1));
}
void main()
{
            int i,n,term=1;
            float x,y,sum;
            clrscr();
            printf("\n Enter No. of Terms N [1-12] : ");
            scanf("%ld",&n);
            if(n>=1 && n<=12)
            {
                        printf("\n Enter the Value of 'X' in degrees : ");
                        scanf("%f",&y);

                        x=y*(PI/180.00); /* Converting degrees to radians */

                        sum=1;
                        for(i=1;term<=n;i++)
                        {
                                    sum+=pow(x,i)/(float)fact(i);
                                    term++;
                        }
                        printf("\n e^%.2f = %f",y,sum);
            }
            else
                        printf("Wrong entry!!!");
            getch();
}




/*         =====OUT PUT=====

       1.Enter No. of Terms N [1-12] : 8

             Enter the Value of 'X' in degrees : 60

             e^60.00 = 2.850986

       2.Enter No. of Terms N [1-12] : 12

             Enter the Value of 'X' in degrees : 90

             e^90.00 = 4.813863

       3.Enter No. of Terms N [1-12] : 9

             Enter the Value of 'X' in degrees : 0

             e^0.00 = 1.000000

0 comments:

Post a Comment