CS 6 Homework Assignment 2
Early Due: Friday, September 19 at 6 PM.
Due: Sunday, September 21 at 6 PM.
Introduction
This is a straight programming assignment with 3 programs.
As always, send e-mail to me directly (mike@cs.hmc.edu) if you have
any problems.
Programming Assignment
The Fibonacci numbers are a series of numbers
that frequently arise in math and computer science.
The Fibonacci series begins with 0 and 1 and has
the property that each subsequent Fibonacci number is the
sum of the two previous Fibonacci numbers.
As a little research,
find out who Fibonacci was and to what
question this series was originally proposed
as the answer.
You can add this
information as part of your program comments.
-
Problem 1:
Write a
while
loop based program that, given an integer N
(entered by the user)
will print the Nth fibonacci number,
(N=0...MAXINT,
N=0 will return 0,
N=1 will return 1,
N=2 will return 1, etc.)
The program should also loop
(using a
while
to take repeated entries from the user).
When the user types
-l
as their input the program should stop.
-
Problem 2:
Rewrite the previous program so that the loop
which does the actual computation is a
for
loop.
-
Problem 3:
A right triangle can have sides that are all integers.
The set of three integer values for the sides of a right triangle
is called a Pythagorean triple.
These three sides must satisfy the relationship that the sum
of the squares of two of the sides is equal to the square of
the hypotenuse (3rd side).
Find all Pythagorean triples for
side1, side2,
and the
hypotenuse
all no larger than 100.
Use a triple-nested
for-loop that tries all possibilities.
Note:
also remove duplicates, e.g, remove one of 3,4,5 or 4,3,5.
Homework Goals
- Make sure that you can follow directions - note, you are to
use for and while loops, not recursion.
- Make sure that basic java programming becomes familiar.
- Find and use the Math class.
- Use HMCSupport to format output data.
- Use the submit program to submit your homework.
Last modified September 12, 1997 by mike@cs.hmc.edu