Sine,Cosine & Exponential Series

6. Program to evaluate Sine Series, Cosine Series and Exponential Series?

    Sine series               : sin x = x - ((x^3)/3!) + ((x^5)/5!) - …………..
    Cosine Series          : cos x = 1 - ((x^2)/2!) + ((x^4)/4!) -…………...
    Exponential Series  : e^x  = 1 + (x/1!) + ((x^2)/2!) + ((x^3)/3!)+…….

Algorithm

Step 1:Start
Step 2:Declare variables i,n,j,ch as integers ;x,t,s,r as float and c=’y’ as char.
Step 3:while c=’y’ repeat steps 4 to 13
Step 4:Print the menu 1.Sine 2.Cosine 3.Exponential series.
Step 5:Input and read the choice,ch.
Step 6:If ch=1 then
                        6.1)Input and read the limit of the sine series,n.
                        6.2)Input and read the value of  angle(in degree),x.
                        6.3)Convert the angle in degree to radian,x=((x*3.1415)/180)
                        6.4)Assign t=r and s=r.
                        6.5)Initialize i as 2.
                        6.6)Initialize j as 2.
                        6.7)while(j<=n)  repeat the steps a to d.
                                    a)Find t=(t*(-1)*r*r)/(i*(i+1))
                                    b)Find s=s+t
                                    c)Increment i by 2.
                                    d)Increment j by 1.
                        6.8)End while.
                        6.9)Print the sum of the sine series,s.
Step 7:End if.
Step 8:Else If the ch=2 then
                        8.1)Input and read the limit of the cosine series,n.
                        8.2)Input and read the value of  angle(in degree),x.
                        8.3)Convert the angle in degree to radian,x=((x*3.1415)/180)
                        8.4)Initialize t as 1,s as 1 and i as 1.
                        8.5)Initialize j as 2.
                        8.6)while(j<=n)  repeat the steps a to d.
                                    a)Find t=(t*(-1)*r*r)/(i*(i+1))
                                    b)Find s=s+t
                                    c)Increment i by 2.
                                    d)Increment j by 1.
                        8.7)End while.
                        8.8)Print the sum of the cosine series,s.
Step 9:End if.
Step 10:Else If ch=3 then
                        10.1)Input and read the limit of the exponential series,n.
                        10.2)Input and read the value of  power of exponential series,x.
                        10.3)Initialize t as1 and s as 1.
                        10.4)Initialize i as 1.
                        10.5)while(i<n)  repeat the steps a to c.
                                    a)Find t=(t*x)/i
                                    b)Find s=s+t
                                    c)Increment i by 1.
                        10.6)End while.
                        10.7)Print the sum of the exponential series,s.
Step 11:End if.
Step 12:Print ‘wrong choice’.
Step 13:End while.
Step 14:Stop.

Program


#include<stdio.h>
#include<conio.h>
void main()
            {
            int i,n,j,ch;
            float x,t,s,r;
            char c='y';
            clrscr();
            do
            {
                printf("\n1.SINE SERIES");
                printf("\n2.COSINE SERIES");
                printf("\n3.EXPONENTIAL SERIES");
                printf("\n ENTER THE CHOICE");
                scanf("%d",&ch);
                switch(ch)
                {
                  case 1:
                             printf("\nENTER THE LIMIT");
                             scanf("%d",&n);
                             printf("\nENTER THE VALUE OF x:");
                             scanf("%f",&x);
                             r=((x*3.1415)/180);
                             t=r;
                             s=r;
                             i=2;
                             for(j=2;j<=n;j++)
                             {
                                    t=(t*(-1)*r*r)/(i*(i+1));
                                    s=s+t;
                                    i=i+2;
                             }
                             printf("\nSUM OF THE GIVEN SINE SERIES IS %4f",s);
                             break;
                  case 2:
                             printf("\nENTER THE LIMIT ");
                             scanf("%d",&n);
                             printf("\nENTER THE VALUE OF x:");
                             scanf("%f",&x);
                             r=((x*3.14)/180);
                             t=1;
                             s=1;
                             i=1;
                             for(j=2;j<=n;j++)
                             {
                                    t=((-1)*t*r*r)/(i*(i+1));
                                    s=s+t;
                                    i=i+2;
                             }
                             printf("\n SUM OF THE COSINE SERIES IS %f",s);
                             break;
                  case 3:
                             printf("\nENTER THE LIMIT");
                             scanf("%d",&n);
                             printf("\nENTER THE VALUE OF x:");
                             scanf("%f",&x);
                             t=1;
                             s=1;
                             for(i=1;i<n;i++)
                             {
                                    t=(t*x)/i;
                                    s=s+t;
                             }
                             printf("\nSUM OF EXPONENTIAL SERIES IS %f",s);
                             break;
                  default:
                             printf("\n WRONG CHOICE");
                }
                printf("\n DO U WANT TO CONTINUE Y/N");
                scanf("%c",&c);
            }
            while(c=='y');
            getch();
            }

Output


1.SINE SERIES
2.COSINE SERIES
3.EXPONENTIAL SERIES
ENTER THE CHOICE          1

ENTER THE LIMIT  2

ENTER THE VALUE OF x:60

SUM OF THE GIVEN SINE SERIES IS 0.855787
DO U WANT TO CONTINUE Y/N             y

1.SINE SERIES
2.COSINE SERIES
3.EXPONENTIAL SERIES
ENTER THE CHOICE          2

ENTER THE LIMIT  2

ENTER THE VALUE OF x:60

SUM OF THE COSINE SERIES IS 0.452245
DO U WANT TO CONTINUE Y/N             y

1.SINE SERIES                                                                 
2.COSINE SERIES                                                               
3.EXPONENTIAL SERIES                                                           
ENTER THE CHOICE          3
                                                                              
ENTER THE LIMIT  2                                                              
                                                                               
ENTER THE VALUE OF x:3                                                        
                                                                              
SUM OF EXPONENTIAL SERIES IS 4.000000                                         
DO U WANT TO CONTINUE Y/N             n                                             

No comments:

Post a Comment