Implementing Loops to Generate the First N Odd Numbers
An odd number is any integer that, when divided by 2, gives a remainder of 1. Generating the first N odd numbers is similar to even numbers, but we adjust our multiplication:
Example: Generate First N Odd Numbers
public class FirstNOddNumbers {
public static void main(String[] args) {
int N = 10; // Specify how many odd numbers to generate
System.out.println("First " + N + " Odd Numbers:");
for (int i = 0; i < N; i++) {
System.out.print((i * 2) + 1 + " ");
}
}
}
Output:
First 10 Odd Numbers:
1 3 5 7 9 11 13 15 17 19
This example uses the same structure as the even numbers program, with minor adjustments to calculate odd numbers by using (i * 2) + 1.