Labs

This chapter includes exercises for you to work on both during the lab session in class, and later, on your own time.

  1. Write a program that uses a for loop to print out the first 16 multiples of 2.

(Solution: Mult2.java)

Make a copy of your solution and modify it so that, in addition to printing the first 16 multiples of 2, it also prints the first 16 powers of 2. Have both the multiple and the power print on the same line.

(Solution: Pow2.java)

  1. Modify your temperature conversion program from the previous section and use a loop that will allow for 5 different temperatures to be input and converted.

(Reference: FahrCelsReference.java)

(Solution: LoopFahrCels.java)

  1. Write a program that prints the factorials of the integers from 1 to 10. The factorial of a number N is the product of all numbers less than or equal to N: factorial 1 (1!) is just one. 2! is (1 * 2) = 2.
a tabular format:
1! = 1
2! = 2
3! = 6
4! = 24
...

When you get your program working, experiment: What's the highest factorial the computer can correctly calculate?

(Solution: Factorial.java)


Prev -- Up