This preview shows pages 1–2. Sign up to view the full content.
1
CSci 119: Spring 2010
Dr. C. Shin
Lab #9: Repetition Statements
March 29, 2010 (Monday)
InLab:
1.
Write a Java application program that computes the following sums using a
for
loop. Name your
program file “Loops1.java”. Remember to print the results using the standard output. Do not modify
what is already given to you in the program, Loops1.java, and only add your code where it says
“INSERT YOUR CODE HERE”.
(1)
3 + 6 + 9 + … + 96 + 99
initial value = 3, final value = 99, and step = +3
(2)
1 + 1/3 + 1/5 + 1/7 + … + 1/97 + 1/99
initial denominator = 1, final denominator = 99, and denominator step = +2
Hints
: Here, the numerator is always 1. What changes is the denominator so that the
statement being repeated is 1/i where i is the loop control variable.
Integer divisions result in integer answers. For example 1/2 is 0.
If you want an
answer 0.5 (which is a double value) for 1/2, the division must be a double
division.
Hence, the repeated statement , 1/i, which is an integer division, should be a
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '09

Click to edit the document details