Write a Java program to print first 10 odd natural numbers using for loop.
package NumPrograms; public class First10OddNaturalNum1 { public static void main(String[] args) { System.out.println("The First 10 Odd Natural Numbers are"); for(int i = 1; i <= 10; i++) { System.out.println(2 * i - 1); } } }
This Java program prints the first 10 odd natural numbers using a while loop.
package NumPrograms; public class First10OddNaturalNum2 { public static void main(String[] args) { int i = 1; System.out.println("The First 10 Odd Natural Numbers are"); while( i <= 10) { System.out.println(2 * i - 1); i++; } } }
The First 10 Odd Natural Numbers are
1
3
5
7
9
11
13
15
17
19
This Java example code uses the do while loop to print the first 10 odd natural numbers.
package NumPrograms; public class First10OddNaturalNum3 { public static void main(String[] args) { int i = 1; System.out.println("The First 10 Odd Natural Numbers are"); do { System.out.println(2 * i - 1); } while(++i <= 10); } }
The First 10 Odd Natural Numbers are
1
3
5
7
9
11
13
15
17
19
Please refer to
- Print the First 10 Even Natural Numbers
- Print the First 10 Natural Numbers
- Print the First 10 Natural Numbers in Reverse
- Java program to Print Even Numbers from 1 to N
- Java program to Print Odd Numbers from 1 to N
- Java program to Print Natural Numbers from 1 to N
- Java program to Print Natural Numbers in Reverse Order
- Java Program to Find the Sum of Natural Numbers from 1 to N