Print Odd Numbers
Print all odd numbers in a given range.
BeginnerTopic: Module 3: Loop Programs
Java Print Odd Numbers Program
This program helps you to learn the fundamental structure and syntax of Java programming.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 100; i += 2) {
System.out.print(i + " ");
}
}
}Output
1 3 5 ... 99
Understanding Print Odd Numbers
We start from 1 and increment by 2 to hit only odd numbers.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.