# Java Program to Display Odd Numbers From 1 to 100

In this section, we will create a Java program to display odd numbers from 1 to 100. To learn the Java odd number program, you must have the basic knowledge of Java for loop and if statement.

We can use different Java loops to display odd numbers:

• Using Java for Loop
• Using nested-if Statement
• Using while Loop

## Using Java for Loop

In the following example, we have declared a variable named number and initialized it with 100 (the limit to print the odd number). We have used a for loop that executes up to 100 times and for each iteration of i the if statement checks the number is odd or not. After printing each odd number, the value if i is increased by 1.

In order to check the number, we have divided the number by 2 if it leaves a remainder, the number is odd and the print statement prints that number.

DisplayOddNumbersExample1.java

1. public class DisplayOddNumbersExample1
2. {
3. public static void main(String args[])
4. {
5. int number=100;
6. System.out.print(“List of odd numbers from 1 to “+number+”: “);
7. for (int i=1; i<=number; i++)
8. {
9. //logic to check if the number is odd or not
10. //if i%2 is not equal to zero, the number is odd
11. if (i%2!=0)
12. {
13. System.out.print(i + ” “);
14. }
15. }
16. }
17. }

Output:

```List of odd numbers from 1 to 100: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
```

## Using nested-if Statement

The following program is slight different from the above program because we have defined a method that contains the logic to check odd number. Inside the method, we have used nested-if statement.

DisplayOddNumbersExample2.java

1. public class DisplayOddnumbersExample2
2. {
3. public static void main(String[] args)
4. {
5. System.out.println(“List of odd numbers: “);
6. //method calling
7. displayOddNumbers(1, 100);
8. }
9. //method that checks the number is odd or not
10. private static void displayOddNumbers(int number, int end)
11. {
12. if(number>end)
13. return;
14. if(number%2!=0)
15. {
16. //prints the odd numbers
17. System.out.print(number +” “);
18. //calling the method and increments the number by 2 if the number is odd
19. displayOddNumbers(number + 2, end);
20. }
21. else
22. {
23. //increments the number by 1 if the number is odd
24. displayOddNumbers(number + 1, end);
25. }
26. }
27. }

Output:

```List of odd numbers:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
```

## Using while Loop

In the following program, we have replaced the for loop with while loop.

DisplayOddNumbersExample3.java

1. import java.util.Scanner;
2. public class DisplayOddNumbersExample3
3. {
4. public static void main(String[] args)
5. {
6. int number, i;
7. Scanner sc=new Scanner(System.in);
8. System.out.print(“Enter the limit: “);
9. number = sc.nextInt();
10. i=1;
11. System.out.print(“Lit of odd numbers: “);
12. //the while loop executes until the cond\ition become false
13. while(i<=number)
14. {
15. //prints the odd number
16. System.out.print(i +” “);
17. //increments the variable i by 2
18. i=i+2;
19. }
20. }
21. }

Output:

```Enter the limit: 100
Lit of odd numbers: 1 3 5 7 9 11 15 17 13 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 83 81 85 87 89 91 93 95 97 99```