How can I write a program to find the sum of the series given below:-
((1)/(2))+((3)/(4))+((5)/(6))+((7)/(8))+((9)/(10))......n

I have written a code but it is not giving the desired result...

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float sum=0,i;
printf("Enter nthe term");
scanf("%d",&n);

for(i=0;i<n;i++)
{
    sum=sum+((i+1)/(i+2));
}
printf("sum=%f",sum);
getch();
}

Please help...

I have written a code but it is not giving the desired result...

Then you'd better put a few printf's in your for loop to figure out what is going wrong.

Yep its is performing this sequence 1/2 + 2/3 + 3/4 + 4/5 + ....
where as my sequence have to be in this form 1/2 + 3/4 + 5/6 + 7/8....
so what should i do to get the result ...
But now after I made some changes in the loop .I am getting error Stating..Floating point stack over flow..????
here is the code:.

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

void main()
{

int n;
float sum=0,i,y;

printf("Enter nthe term ");
scanf("%d",&n);

for(i=0;i<n*2;i++)
     {    y=fmod(i,2);
          if(y==0.000000)
          {
                sum=sum+((i+1)/(i+2));
          }
     }

printf("sum=%f",sum);

getch();

}

In your original code you have: for (i = 0; i < n; i++) which increments i by 1 for each iteration.
The solution is to increment i by 2 for each iteration.
i += 2 // equivalent to i = i + 2

A few notes:

  • You should always declare the main() function as type int, and return a value at the end of it. While some compilers do allow you to declare void main(), it is non-standard and non-portable. Don't do it.
  • The <conio.h> header and the functions it declares are not part of the standard library, and even for those few compilers which support it, vary greatly in the specific functions it supports. Avoid using it. In any case, a good modern IDE shuch as Code::Blocks shouldn't close the program window off immediately after the program is finished, so the getch() kludge shouldn't be needed.
  • Using a float to hold an integer variable is problematic, as floats do not have an exact represention for certain integer values. The variable i should be type int in this case, and cast to float when the division is made.
  • The whole section using the variable y is unnecessary, and since y is never initialized, it actually causes the computation to fail. You can safely remove the entire thing.

Taking all this into account, the follow should do what you want:

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

int main()
{

    int n, i;
    float sum = 0;

    printf("Enter nth term ");
    scanf("%d", &n);

    printf("sum = ");
    for(i = 1; i < n * 2; i += 2)
    {
        if (i > 1)
        {
            printf(" + ");
        }

        sum += i / (float) (i + 1);
        printf("%d/%d", i, i + 1);
    }

    printf(" = %f\n", sum);

    return 0;
}

Edited 3 Years Ago by Schol-R-LEA

Thanxx a lot sir for your Help.Can you please clarify my doubt ..
I had used fmod() and it ws sgiving me error Stack over flow..Why is that so.
and what is the meaning of (float) in the middle of the loop.

The (float) part is a cast; it converts the integer value of (i + 1) (note how I had changed i to an int ealier?) to a float value. The reason I did this was because otherwise it would have been compilede as integer division, which wouldn't give the desired values.

As for the += operator, it is what is called the accumulate-addition operator, and what it does is it adds the value on the right-hand side to the variable on the left-hand side. it is a shorthand for

sum = sum + (i / (float) (i + 1));
This question has already been answered. Start a new discussion instead.