Unit 2 Practice: Java Iteration¶
Name: ____________
Write each solution in Java. Use iteration (
for,while, ordo-while) as the main approach. Do not use recursion. For each problem, create apublic class Mainand test your code inmain.
Problem 1. Sum from 1 to N¶
Write a program that reads an integer n and prints the sum of all integers from 1 to n.
Input
10
Output
55
Requirements
- Use a loop.
- If
nis less than1, print0.
Problem 2. Count Even Numbers¶
Write a program that reads two integers start and end, then prints how many even numbers are in that range.
The range includes both start and end.
Input
3 12
Output
5
Explanation
The even numbers are 4, 6, 8, 10, 12.
Requirements
- Use a loop.
- Your program should also work when
startis greater thanend.
Problem 3. Factorial¶
Write a program that reads an integer n and prints n!.
n! means:
n * (n - 1) * (n - 2) * ... * 1
Input
5
Output
120
Requirements
- Use a loop.
- Use
longfor the result. - If
nis0, the answer is1. - Assume
0 <= n <= 20.
Problem 4. Prime Number Check¶
Write a program that reads an integer n and prints whether it is a prime number.
A prime number is greater than 1 and has only two positive divisors: 1 and itself.
Input
17
Output
prime
Another Input
21
Another Output
not prime
Requirements
- Use a loop to check divisors.
1is not a prime number.- Try to stop the loop early when you already know the answer.
Problem 5. Print All Prime Numbers in a Range¶
Write a program that reads two integers start and end, then prints all prime numbers in that range.
The range includes both start and end.
Input
10 30
Output
11 13 17 19 23 29
Requirements
- Use nested loops.
- Your program should also work when
startis greater thanend. - Print the prime numbers on one line separated by spaces.
Problem 6. Multiplication Table¶
Write a program that reads an integer dan and prints its multiplication table from 1 to 9.
Input
7
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
Requirements
- Use a loop.
- Match the output format exactly.
Problem 7. Sum of Digits¶
Write a program that reads a positive integer n and prints the sum of its digits.
Input
5079
Output
21
Explanation
5 + 0 + 7 + 9 = 21
Requirements
- Use a
whileloop. - Use
%and/to separate digits. - Assume
n > 0.
Problem 8. Reverse a Number¶
Write a program that reads a positive integer n and prints the number with its digits reversed.
Input
12340
Output
4321
Requirements
- Use a loop.
- Use
%and/to process digits. - It is okay if leading zeroes disappear after reversing.
Problem 9. Greatest Common Divisor¶
Write a program that reads two positive integers a and b, then prints their greatest common divisor.
The greatest common divisor is the largest integer that divides both numbers.
Input
24 36
Output
12
Requirements
- Use a loop.
- You may use either a simple divisor search or the Euclidean algorithm.
- Assume
a > 0andb > 0.
Problem 10. Fibonacci Sequence¶
Write a program that reads an integer n and prints the first n Fibonacci numbers.
The Fibonacci sequence starts like this:
0 1 1 2 3 5 8 13 ...
Input
8
Output
0 1 1 2 3 5 8 13
Requirements
- Use a loop.
- Assume
1 <= n <= 45. - Print the numbers on one line separated by spaces.
Problem 11. Star Triangle¶
Write a program that reads an integer n and prints a left-aligned triangle of stars.
Input
5
Output
*
**
***
****
*****
Requirements
- Use nested loops.
- Assume
1 <= n <= 20.
Problem 12. Number Pyramid¶
Write a program that reads an integer n and prints a centered number pyramid.
Input
4
Output
1
121
12321
1234321
Requirements
- Use nested loops.
- Use spaces to align the pyramid.
- Assume
1 <= n <= 9.