I need some help: how can I do this:

Write a program to find out how many terms of the series:

1 + 1/2 + 1/3 + 1/4 +.... are needed before the total first exceeds 5.

The first step to figuring out how to write the program is figuring out how to write the algorithm. How would you solve this problem with pencil and paper? Where would you start? What steps would you take?

Use floating point, otherwise the answer is 1.

1 + 1/2.0 + 1/3.0 + 1/4.0

u need an array
float x[20];
// to enter the codes now.use a for loop
int i,y=1;
for (i=0;i<20,i++)

x=1/y+1/y+1;


// to calculate total
for(i=0;i<20;i++)
total=x+x[i+1];
if (total>5) num=x// declare num first somewhere in the program
// how many terms needed

of course this is not the absolute code but i guess you are intellighent enough to go ahead
before writing any program do it on paper first
get the logic first then use your computer
:lol: :cool:

>u need an array

No, you don't. In fact, it would be better not to.

>int i,y=1;
>for (i=0;i<20,i++)
>x=1/y+1/y+1;

Reread my previous post.

well dave please clarify the reason. coz i am not an expert in c++. thanks in advance

all you need is a loop, a float to hold the result, and a loopcounter :)

I wrote the code in about 3 minutes, including the time to start my editor and the time to compile it.

well dave please clarify the reason. coz i am not an expert in c++. thanks in advance

The array? Because then you limit yourself to the array size. Without using an array I found that it took about 84 iterations to pass 5. So an array of 20 would not have been adequate.

Or the integer math? In integer math, 1 / 2 = 0. But floating point math, 1.0 / 2 = 0.5. If you take the integer result 0 and (implicitly) convert to floating point, you will have 0.0. You can see how this would seriously affect the calculation.

yeah sort of understanding a little bit. jwenting said something about a counter can you please clarify that ... point.
thnx in advance

yeah sort of understanding a little bit. jwenting said something about a counter can you please clarify that ... point.
thnx in advance

Note the denominator: 1, 2, 3, ... Seems a perfect candidate for a counter.

This is hard not to completely give away since it's only about 4 lines of code -- why not post an attempt?

ok i will try but not now. i nedd if some more time to figure it out
else i guess i only have to run the bulldozer somewhere out there!!!!

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.