Java Program to Display Odd Numbers

In this tutorial, we shall write Java Programs that print all odd numbers from starting of 1, up to the given limit or maximum.

You can use looping techniques, to iterate for each odd number until a threshold, or maximum. We shall use for loop and while loop to iterate over the even numbers up to we reach the end.

Or you can iterate from 1 to n, in steps of 1, and check if the number is odd during each iteration.

Let us write algorithms for these two approaches.

Algorithms to Print Even Numbers up to N

Algorithm 1 – Increment in steps of 2

Following is an algorithm using which we can print even numbers up to by incrementing the loop variable with 2.

  1. Start.
  2. Take a value for n. This is our upper limit for the even numbers we print to console.
  3. Initialize variable odd with 1. Odd numbers start from 1.
  4. Check if odd is less than or equal to n.
  5. If the condition is true, go to step 6, else go to step 9.
  6. Print odd.
  7. Increment odd by 2. So, that odd has the next odd number now in the next iteration.
  8. Go to step 4.
  9. Stop.

Algorithm 2 – Check if Number is Odd

Following is an alternative way to print odd numbers up to n by checking during each iteration if the number is odd.

  1. Start.
  2. Take a value for n. This is our upper limit for the even numbers we print to console.
  3. Initialize variable i with 1.
  4. Check if i is less than or equal to n.
  5. If the above condition is true, go to step 6, else go to step 11.
  6. Check if i leaves a reminder of 1 when divided by 2.
  7. If the above condition is true, go to step 8, else go to step 9.
  8. Print i.
  9. Increment i by 1.
  10. Go to step 4.
  11. Stop.

We shall go through each of these algorithms in our examples, using different looping mechanisms.

Example 1 – Display Odd Numbers up to N – While Loop

Java Program – Using Algorithm 1

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		int odd=1;
		while (odd<=n) {
			System.out.print(odd+"  ");
			odd += 2;
		}
	}
}

Java Program – Using Algorithm 2

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		int i=1;
		while (i<=n) {
			if(i%2==1) {
				System.out.print(i+"  ");
			}
			i++;
		}
	}
}

Output

Run any of the above program, and we shall get all the odd numbers up to n, printed to the console.

1  3  5  7  9  11  13  15  17  19

Example – Display Odd Numbers up to N – For Loop

Java Program 2 – Using Algorithm 1

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		for (int i=1; i<=n; i+=2) {
			System.out.print(i+"  ");
		}
	}
}

Java Program – Using Algorithm 2

</>
Copy
/**
 * Java Program - Display Odd Numbers
 */

public class DisplayOddNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all odd numbers <=n 
		for (int i=1; i<=n; i++) {
			if(i%2==1) {
				System.out.print(i+"  ");
			}
		}
	}
}

Output

Run the above program, and we shall get all the odd numbers up to n, printed to the console.

1  3  5  7  9  11  13  15  17  19

Conclusion

In this Java Tutorial, we learned how to print all the odd numbers up to a given number.