Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Functions, real numbers and for loops

1. ## Functions, real numbers and for loops

Hi, I have this Java problem and I don't completely understand what it means, this is the problem:

Write a function named sumTo() that takes an integer parameter n and that returns the sum of the first n reciprocals. In other words:

sumTo(n) returns (1 + 1/2 + 1/3 + 1/4 + ... + 1/n)

(of course, 1/2 in this case means .5) For example, sumTo(2) should return the value 1.5 (1 + 1/2). If sumTo() is passed the value 0, it should return 0.0 as its result. You may assume that sumTo() is never passed a negative value.

Thanks

2. ## Re: Functions, real numbers and for loops

Learn how to use loops.
Also note that
1 + 1/2 + 1/3 + 1/4 + .... = 1/1 + 1/2 + 1/3 + ... = sum(1/i, for i = 1 to n)
Your mission is to write the last expression in for loops which should be pretty easy.