How to add 1/1+ 1/2+1/3...+1/9+1/10 .
I tried but I am getting answer in negative number.
bbabarajj 0 Newbie Poster
Recommended Answers
Jump to Post— jonsca 1,059Add 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.
Jump to Post— vmanes 1,165If 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.
Jump to Post— vmanes 1,165It 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 …
All 9 Replies
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
vmanes 1,165 Posting Virtuoso
EngSara -2 Light Poster
bbabarajj 0 Newbie Poster
NathanOliver 429 Veteran Poster Featured Poster
EngSara -2 Light Poster
vmanes 1,165 Posting Virtuoso
vmanes 1,165 Posting Virtuoso
bbabarajj 0 Newbie Poster
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.