Series output problem

This is a discussion on Series output problem within the C Programming forums, part of the General Programming Boards category; I need to print the following series : 1-1/2+1/3-1/4......+-1/n where n will be given by user. I have written the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Series output problem

    I need to print the following series :

    1-1/2+1/3-1/4......+-1/n

    where n will be given by user.

    I have written the code as :

    Code:
    int main()
    {
         int i,n;
         float sum=0.0;
         printf("\n Enter the no. of terms for summation : ");
         scanf("%d",&n);
         for(i=1;i<=n;i++)
           {
               if(i%2==0)
                  i=-i;
               sum=sum+1.0/i;
           }
         printf("\n Summation is : %4.2f",sum);
         
         return 0;
     }

    Only for n=1 I am getting an output, but frm n=2 onward no output.

    Please tell me where i am going wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Code:
    for(i=1;i<=n;i++)
    ...
                  i=-i;
    What do you think is going to happen to your for loop, if you negate i ?


    Try this code and see what happens
    Code:
    int sign = 1;
    for ( i = 0 ; i < 10 ; i++ ) {
        printf("Sign = %d\n", sign );
        sign = -sign;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Code:
    for(i=1;i<=n;i++)
    ...
                  i=-i;
    What do you think is going to happen to your for loop, if you negate i ?
    Oppss....That was a pathetic mistake i made..!!!

    Try this code and see what happens
    Code:
    int sign = 1;
    for ( i = 0 ; i < 10 ; i++ ) {
        printf("Sign = %d\n", sign );
        sign = -sign;
    }
    Thankss Salem...It worked out beautifully for me...Thank u so so much...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. series problem stucked in the last step
    By ramim121 in forum C Programming
    Replies: 3
    Last Post: 07-03-2010, 04:52 PM
  2. HELP: Series of Multiplication Problem...
    By Ocin101 in forum C Programming
    Replies: 13
    Last Post: 07-20-2009, 08:38 AM
  3. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 10:57 AM
  4. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 01:39 PM
  5. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:32 PM

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