Powers of Two
Completed
Read a limit L and print every power of two that is ≤ L, one per line.
Input
One integer ≥ 1.
Output
One power per line.
Example
Input
100
Output
1 2 4 8 16 32 64
Editor
Java
Hints
The update part of a for loop can be any statement, like p *= 2.
Solution explanation
A for loop doesn't have to count by one.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long limit = sc.nextLong();
for (long p = 1; p <= limit; p *= 2) {
System.out.println(p);
}
}
}