While Loop Labs - MATES-CS

advertisement
Computer Science – While Loop Labs
1. You deposit $2500.00 into a Certificate of Deposit (CD) at your bank. Write a program to
calculate how many years it will take for your CD to be worth $5000.00 or more, if the CD pays
7.5% interest compounded annually. (CDInterest.java)
2. Write a program that takes a non-negative integer as input and displays each of the digits on
a separate line. The program (VerticalInt.java) output should look similar to:
Enter an integer: 41345
4
1
3
4
5
3. An interesting problem in number theory is the “necklace problem”. The problem begins
with two single digit numbers. The next number is obtained by adding the first two numbers
together and saving only the units digit. This process is repeated until the “necklace” closes by
returning the original two numbers. For example, if the starting numbers are 1 and 8, twelve
steps are required to close the “necklace”.
1 8 9 7 6 3 9 2 1 3 4 7 1 8
Write a program that asks the user for two starting numbers, and then displays the sequence
and the number of steps taken. (Necklace.java)
4. Write a program that asks the user for a non-negative integer and then displays a message
indicating if it is prime or not. (PrimeNum.java)
5. Modify the program for #4 and allow the user to enter in two non-negative integers and
have your program display how many primes are within the range. (PrimeNum2.java) Example
output:
Enter starting number: 50
Enter final number: 59
Number of primes in range: 2
6. Write a program that displays the sum of all of the integers between two input numbers,
inclusive. The program (SumBetween.java) output should look similar to:
Enter starting number: 25
Enter ending number: 47
The sum of integers from 25 to 47 is 828
7. Write a program that reads a positive integer and displays its factorial. 0! is defined to be 1,
and all negative factorials are undefined. Make sure your program checks the input from the
user, and continues to ask until valid input has been entered. (Factorial.java)
8. Write a program that displays the prime factors of an integer entered by the user. If a
number is a prime factor more than once, it should be printed more than once. The program
output should look similar to: (PrimeFactors.java)
Enter a prime number: 140
Prime factors: 2, 2, 5, 7
9. Write a program that plays a guessing game where the computer tries to guess a number
picked by the user. After each guess, the user must tell the computer if it was correct, too high
or too low. The program should also count the guesses. The program output should look like:
Think of a number between 1 and
Is the number 50 (Correct, Low,
Is the number 25 (Correct, Low,
Is the number 13 (Correct, Low,
Is the number 19 (Correct, Low,
Number of guesses: 4
100 and
High)?
High)?
High)?
High)?
then press any key.
h
h
l
c
Hint: Maintain highestPossible and lowestPossible variables and always guess midway between
the two. This is called a binary search. (GuessNum.java)
10. Write a program that acts like a simple slot machine. The user starts with 100 tokens, and
with each pull of the handle loses 1 token. The computer spins three wheels, each consisting of
the numbers 1, 2, and 3. If all three numbers are 1’s, the user gets 4 tokens, if all three
numbers are 2’s, the user gets 8 tokens and if all three numbers are 3’s, the user gets 12
tokens. Program output should look like: (Slots.java)
You have 100 tokens. Pull (Y/N)? Y
[1] [3] [2]
You lose!
You have 99 tokens. Pull (Y/N)? Y
[2] [2] [2]
You win 8 tokens!
You have 106 tokens. Pull (Y/N)? N
Thanks for playing!
Download