How to add 1/1+ 1/2+1/3...+1/9+1/10 .
I tried but I am getting answer in negative number.

Dani AI

Generated

Good catches by , , and . Once your code is doing floating‑point division and your loop does not include 0, the remaining pitfalls are mostly tooling and precision. Turn on warnings so the compiler flags issues early: g++ -Wall -Wextra -Wconversion -pedantic (or MSVC /W4 /permissive-). Also, prefer a standard-conforming entry point: int main() { ... } with a return 0; at the end. If you only need the value once, ’s approach is fine; if you want a numerically sturdier summation (useful as n grows), see below.

#include <iostream>
#include <iomanip>

long double harmonic_1_to_10_kahan() {
    long double sum = 0.0L, c = 0.0L;
    for (int i = 10; i >= 1; --i) {            // add small terms first
        long double y = 1.0L / i - c;          // Kahan compensated summation
        long double t = sum + y;
        c = (t - sum) - y;
        sum = t;
    }
    return sum;
}

int main() {
    long double sum = harmonic_1_to_10_kahan();
    std::cout << std::setprecision(17) << sum << '\n';
}

This sums smaller terms first (1/10 up to 1/1) and uses Kahan compensation to reduce round‑off drift in floating point. (en.wikipedia.org)

For a quick correctness check, the expected result for 1/1 + 1/2 + ... + 1/10 is about 2.928968253968254 (the 10th harmonic number, H10). If your output differs wildly, recheck operator usage, loop bounds, and that your accumulator is a floating type. (en.wikipedia.org)

Recommended Answers

All 9 Replies

Add 1.0/1.0+1.0/2.0+... when you do integer division (e.g., 1/2) regardless of the variable the result is going into it still still gets truncated. If you haven't already make sure your result is declared as type double otherwise nothing will work.

If you're writing a C++ statement like that, the answer should be 1.

1/1 = 1
1/2 = 0
1/3 = 0
.....
Please show the actual code, and we can give an actual help.

Try this:

double sum=0;
for(double i=1;i<=10;i++)
sum= sum+(1.0/i);

this is what i did but didnt work

#include <iostream>

using namespace std;

void main ()
{
double sum, i;
sum=0;
for (i = 0, sum = 0; i <= 10; i++)
sum = sum +i(1.0\i);
    cout << "Sum is " << sum << endl;
    }

to dived you use / not \. also i(...) is not valid. if you want to multiply i by that quantity you have to do i * (1.0 / i) . Also please use code tags. the link to explian them is http://www.daniweb.com/forums/announcement8-3.html

It didn't work because u are making sum zero in each iteration of the loop .. so just remove sum=0 from the loop.. it's enough to initialize it before the loop.

i.e:
sum=0;
for (i = 0; i <= 10; i++)
sum = sum +i(1.0\i);

sum = sum +i(1.0\i); That's you're killer. First of all, use the correct division symbol: /
Secondly, why is there an 'i' in front of the division?
That statement, corrected, should be:
sum = sum + ( 1.0 / i );

But in the loop's first iteration, you add 1.0 / 0, which is...um....not defined mathematically. In actuality, you get a really huge result.

Start the loop at 1.

It didn't work because u are making sum zero in each iteration of the loop .. so just remove sum=0 from the loop.. it's enough to initialize it before the loop.
i.e:
sum=0;
for (i = 0; i <= 10; i++)
sum = sum +i(1.0\i);

While I agree it looks better to initialize sum outside the loop, it is NOT being reset to 0 each iteration, any more than counter i is. The initialization statement is executed only once.

thankx everyone

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.