jituTechnology

jituTechnology

A program to calculate the sum of the series using function.

A program to calculate the sum of the series using function.

Codes
/*A program to calculate the sum of given series using function*/
/*Programed by          Jitendra Kr Yadav
                         10116
                         2 Mar,2011*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void series1(int, int);
void series2(int, int);
void series3(int, int);
main()
{
      int a,n,choice;
      printf(“Series 1: y=1+x^2/2!+x^4/4!+x^6/6!+…\n”);
      printf(“Series 2: y=x-x^3/3+x^5/5-x^7/7+x^9/9-…\n”);
      printf(“Series 3: y=x^3/5-x^6/10+x^9/15-x^12/20+…\n”);
      printf(“Enter your choice:”);
      scanf(“%d”,&choice);
      printf(“Enter the value of x: “);
      scanf(“%d”,&a);
      printf(“Enter the value of n: “);
      scanf(“%d”,&n);
      switch(choice)
      {
            case 1:
                 series1 (a,n);
                 break;
            case 2:
                 series2 (a,n);
                 break;
            case 3:
                 series3 (a,n);
                 break;
      }
      getch();
}
void series1 (int x,int n)
{
int i, j, fact;
float sum, term;
sum=0;
for(i=1;i<n;i++)
{
for(j=1,fact=1;j<=((2*i)-2);j++)
fact=fact*j;
term=(pow(x,((2*i)-2))*1.0/fact);
sum+=term;
}
printf(“Sum = %.2f.\n”,sum);
printf(“\n\nPress any key to continue…”);
}
void series2 (int x,int n)
{
int i;
float sum, term;
sum=0;
for(i=1;i<n+1;i++)
{
term=(pow(x,((2*i)-1))*(pow(-1,(i+1))))*1.0/((2*i)-1);
sum+=term;
}
printf(“Sum = %.2f.\n”,sum);
printf(“\n\nPress any key to continue…”);
}
void series3 (int x,int n)
{
int i;
float sum, term;
sum=0;
for(i=0;i<n;i++)
{
term=(pow(x,((3*i)+3))*(pow(-1,(i+2))))*1.0/((5*i)+5);
sum+=term;
printf(“%d\n”,term);
}
printf(“Sum = %.2f.\n”,sum);
printf(“\n\nPress any key to continue…”);
}
Output
Series 1: y=1+x^2/2!+x^4/4!+x^6/6!+…
Series 2: y=x-x^3/3+x^5/5-x^7/7+x^9/9-…
Series 3: y=x^3/5-x^6/10+x^9/15-x^12/20+…
Enter your choice: 2
Enter the value of x: 2
Enter the value of n: 3
Sum = 5.73.
Press any key to continue…

No comments yet.

Add a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Connect with Me

GPO 8973 NPC 541; Kathmandu Nepal
Phone: +977 9851236800

Calendar

May 2024
S M T W T F S
 1234
567891011
12131415161718
19202122232425
262728293031