Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.


i cant understand this logic
Posted
Updated 13-Aug-14 6:55am
v2
Comments
PIEBALDconsult 13-Aug-14 12:55pm    
Sounds like homework.

Check out the definition of the Natural Logarithm of 2 here.

Maths is beautiful!
 
Share this answer
 
Comments
CPallini 13-Aug-14 14:48pm    
5.
You are given a rare chance to learn something and use school assistance. Later on, when you start working, you will have less and less time. So, use this chance well. And the only way to learn something is to solve problems all by yourself. This exercise is quite useful. If someone else will develop a solution for you, you will loose yet another chance to learn. You don't want it. Besides, taking advantage over your school fellows by cheating is not good.

[EDIT]

So, start here:
http://en.wikipedia.org/wiki/Taylor_series[^] (mathematics),
http://www.cplusplus.com/reference/cmath/log[^] (for testing the results of your code),
http://www.cprogramming.com/tutorial/c/lesson3.html[^] (programming).

—SA
 
Share this answer
 
v2
Comments
CPallini 13-Aug-14 14:48pm    
5.
Sergey Alexandrovich Kryukov 13-Aug-14 15:41pm    
Thank you, Carlo.
—SA
Hi,

Here i am giving the sample which will give you required result...

Sincere advise don't look for solution from others without initial effort.
int main()
{
C#
double sum=0,fraction;
    int num;
    printf("Enter the Sequence length\n");
    scanf_s("%d",&num);
    for(int i=1;i<=num;i++)
    {
        if(i%2==0)
        {
            fraction = -(float)1/i;
        }
        else
            fraction = (float)1/i;
        sum = sum + fraction;
    }
    printf("The result of the sequence is %f",sum);

return 0;
}
 
Share this answer
 
I SOLVE IT


#include<stdio.h>
void main()
{
int n,k=1;
float sum=1;
printf("Enter a number ");
scanf("%d",&n);

do {
printf("%d",k);

for(;;) {

k++;
if (k>n)
break;
if(k%2==0)
{
printf("- 1/%d",k);
sum=sum-1.0/k;
}
else
{
printf("+ 1/%d",k);
sum=sum+1.0/k;
}

}

}
while (k<=n);
printf(" =%f",sum);

}
 
Share this answer
 
v2
Comments
Philippe Mori 14-Aug-14 18:55pm    
At least, you should format your code correctly.
Rayhanuzzaman Roky 16-Aug-14 4:35am    
yeas thanks

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