Print Odd Numbers in Java
Print all odd numbers in a given range.
BeginnerModule 3: Loop ProgramsExample 3 of 25
print-odd-numbers.java
1public class Main {2 public static void main(String[] args) {3 for (int i = 1; i <= 100; i += 2) {4 System.out.print(i + " ");5 }6 }7}
Output
1 3 5 ... 99
What's going on
We start from 1 and increment by 2 to hit only odd numbers.