006-003-011
Repetition with While Loop
Medium
Problem Description
Repetition with while loop
In this problem, you will create a program that reads an initial value n from Scanner, uses a while loop to repeatedly divide by 2, displays each value, and counts the total number of divisions until reaching 1.
Learning Objective: Understand while loop behavior and implement condition-based loop processing
Overview
The while loop repeats processing while the condition is true. It is suitable when the number of iterations is not known in advance.
Specifications
- Read integer
nfrom standard input - Keep dividing
nby 2 until reaching 1, displaying each value - Count and display the number of divisions
Input
n
n: A positive integer that is a power of 2 (e.g., 2, 4, 8, ..., 1024)
Output Format (when n=16)
16
8
4
2
1
Steps: 4
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up