Thread: sum of series using loops

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    41

    sum of series using loops

    I am trying to use the sum ∑ from i=0 to N of x^(2i+1)/(2i+1)! which is the sum of the series of sinh. now i am able to get the top part of the series but the buttom part i am having issues

    basically this is the part that i have issues with :

    Code:
    for(i = 1;i <= N+1; i = i + 2){
    
    
    for(j=1 ; j <= i; j++)
    {
        f= f * j;
    }
    
    
    sum = sum + pow(x,i)/f;
    
    
    }
     return sum;
    
    
    }

    mainly the 2nd "for".


    and yes I know that c can compute sinh however my homework says that i have to do this so here we are.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    
    
    #define TRUE 1
    #define FALSE 0
    
    
    void getInput(double *N, double *x);
    double sinHyper(double N , double x);
    
    
    
    
    int main()
    {
        double sum;
        double N;
        double x;
        char ans;
        int flag;
    
    
         do
        {
        flag = TRUE;
    
    
             getInput(&N,&x);
        sum = sinHyper(N,x);
        printf("\nsum : %g\n\n\n", sum);
    
    
    
    
             printf("Do you wish to quit (y/n)? ");
        fflush(stdin);
        scanf("%c",&ans);
    
    
              if( ans != 'n')
              {
                 printf("Program terminated \n");
                 flag = FALSE;
              }
        } while(flag == TRUE);
    
    
    }
    
    
    
    
    void getInput(double *N, double *x)
    {
        int flag;// Declaration of variable
    
    
        double N1, x1;
        do
        {
        flag = TRUE;
             printf("Please enter the number of terms N : ");
             scanf("%lf",&N1);
             printf("Please enter the value x : ");
             scanf("%lf",&x1);
    
    
              if( N1 < 0.0)
              {
                  printf("the value of N must be greater than zero.\n");
                  flag = FALSE;
              }
           *N = N1;
           *x = x1;
        } while(flag == FALSE);// Get input from user
    
    
    
    
    }
    
    
    double sinHyper(double N , double x)
    {
        double i , j;
        double sum = 0;
        double f = 1;
        double k = 1;
    
    
    
    
    for(i = 1;i <= N+1; i = i + 2)
    {
    
    
    for(j=1 ; j <= i; j++)
    {
        f= f * j;
    }
    
    
    sum = sum + pow(x,i)/f;
    
    
    }
     return sum;
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    never mind i figured it out dont waste ur time on this

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    90
    You could edit your final post with how you worked it out, just in case anybody else have the same issue.

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    basically this is the part i fixed
    Code:
     
    
    double sinHyper(double N , double x)
    
    {
        double i ;
        double sum = x;
        double t = x;
    
    
    
    
    for(i = 1;i <= N-1; i++)
    
    t= (t*x*x)/((2*i+1)*2*i)
    sum = sum + t
    
    
    }
     return sum;
    
    
    }
    
    

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    93
    Just a side point.

    instead of writing this:

    Code:
    sum = sum + t
    You can write the more concise:

    Code:
    sum += t;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. What is this series?
    By shansajid in forum C Programming
    Replies: 4
    Last Post: 01-15-2013, 05:26 AM
  3. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  4. Replies: 3
    Last Post: 06-01-2011, 04:19 PM

Tags for this Thread