series converges to 2

This is a discussion on series converges to 2 within the C Programming forums, part of the General Programming Boards category; I use a while loop to find the following series converges to 2. 1- 1/3 + 1/5 -1/7 + ... ...

  1. #1
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Question series converges to 2

    I use a while loop to find the following series converges to 2.

    1- 1/3 + 1/5 -1/7 + ...

    My Code is :

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

    void main(void)
    {
    int i=1;
    double single;
    double sum=1.0;

    while ( fabs(single>0.000001) ) { /* exit when single small */
    if (i%2==1)
    single=-1/(2*i+1);
    else
    single=1/(2*i+1);
    sum+=single;
    i++;
    }
    printf("the sum is converges to %lf \n", sum);
    }



    I get the answer 1 instead of the correct one 2.
    I have checked it many times, but i cannot find the error.
    Can someone help me to find out the error.

    Thank you !!

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    #include <stdio.h> 
    #include <math.h> 
    
    /* main returns int */
    void main(void) 
    { 
            /* counters should start with 0 in c*/
            int i=1;                   
            double single; 
            double sum=1.0; 
    
            /* should let .000001 be a #define 
            while ( fabs(single>0.000001) ) { 
                    if (i%2==1) 
                           /* should be single = -1.0/(2.0*i+1.0); 
                               What you have does integer division so you get 
                               single = 0 each time.
                             */
                             single=-1/(2*i+1); 
                    else 
                             /* also needs to be changed */
                             single=1/(2*i+1); 
                    sum+=single; 
                    i++; 
             } 
             printf("the sum is converges to %lf \n", sum); 
    
          /* return 0 */
    }

  3. #3
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Question

    thanks, Nick.

    I try your code already, but I also cannot get the correct answer.

    My code is

    #include <stdio.h>
    #include <math.h>
    #define X 0.000001

    int main(void)
    {
    int i=1;
    double single=0;
    double sum=1.0;

    while ( fabs(single>X) ) {
    if (i%2==1)
    single=-1.0/(2.0*i+1.0);
    else
    single=1.0/(2.0*i+1.0);
    sum+=single;
    i++;
    }
    printf("the sum is converges to %lf \n", sum);

    return 0;
    }


    Anything wrong here ??

  4. #4
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    a:
    your X is too small

    and fabs(single > x) wvaluates to 0 initially bcoz u init single to 0

    try initialising it to 1

    also X needs to be increased

    and
    1- 1/3 + 1/5 -1/7 + ...

    converges to .785897 not 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with this series
    By NoUse in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:57 PM
  2. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  3. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 01:39 PM
  4. array of series
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 06-22-2004, 02:06 PM
  5. taylor series using only stdio.h
    By faruque in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21