Click here to Skip to main content
15,435,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1 - (2/3!) + (3/4!) - (4/5!) + .... ± (n/(n+1)!).

What I have tried:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
for(j=0;j<=n;j++)
{
sum+=2i/i+1;
}
printf("the sum=%d",sum);
}
Posted
Updated 25-Oct-17 18:46pm
Comments
Chris Losinger 25-Oct-17 13:27pm    
What you've posted here won't even compile.
Richard Deeming 25-Oct-17 13:32pm     CRLF
* 2i/i + 1 would mean (2i/i) + 1, not 2i/(i+1); * Except I don't think C lets you multiply a variable by a number without specifying the multiplication operator (2 * i); * 2 * i/(i + 1) is nowhere near the same thing as i/(i + 1)! - multiplying by 2 is not the same as taking the factorial[^] of the divisor; * The sequence is supposed to alternate between adding and subtracting the terms. * And to top if off, you're not even using the loop variable!
Richard Deeming 25-Oct-17 13:35pm    
You should also assume that your teacher is monitoring this site, and is aware of your attempts to cheat on your homework.

Your for loop needs to have i as its variable since you have declared it and not j.

You don't have a question to answer but I'll give some advice anyway. First, write a function to compute the factorial of a value. That will provide the denominator for each value in the series. The rest of this will be a simple division of values and summing the results inside a for loop.

The only semi-tricky thing here is the alternating addition and subtraction. You can do that several ways. One is to use a multiplier and alternate it from +1 to -1. Another is to have a boolean flag that alternates from true to false and tells you whether you should add or subtract the result. Yet another is to determine addition or subtraction based on whether to loop index is odd or even which you can find taking modulo 2 of the loop index.
 
Share this answer
 
I think you should do similar like this.

C++
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
    if(n == 0)
    {
       return 1;
    }
    return n * fact(n-1);
}
void main()
{
    int sum = 1, delta = -1;
    for(int i=2; i<=n; ++i)
    {
       sum += ((i / fact(i + 1)) * delta);
       delta *= -1;
    }
    printf("the sum=%d",sum);
}


I didn't compile the above code. But I think it would help you.
 
Share this answer
 
Comments
Richard MacCutchan 26-Oct-17 4:53am    
Doing people's homework for them does not help them to learn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900