Java Program to Print Odd Numbers From 1 to 99

In this tutorial, we will be going to Solve and tackle the problem Java Program to Print Odd Numbers From 1 to 99. If You Don’t Have an idea on how to Filter out the odd numbers between a given range, then you are at the perfect place to learn.

In this tutorial, we will be focusing on odd numbers concept. We will see how to tackle the above problem.

Java Program to Print Odd Numbers From 1 to 99

As we know that odd numbers are the ones which when divided by 2, it leaves a remainder 1. In this problem, we will be going to Filter out the odd numbers between the given range.

Here, it is between 1 to 99.

So, after every iteration, the counter I moves from 1 to 2 and checks the condition and if it gets satisfied, it Prints else moves to other next iteration.

Initially, we will be looping from 1 to 100, in which we will be making a condition check after each iteration of the loop, whether the present number when divided by 2, leaves a remainder 1, then we will be displaying them from the right first iteration.

As we know, looping can be done anyway, either with a while loop, for loop, or do while any would work here.

In the Below case, I implemented Iteration through for loop. As we know, the remainder is calculated using the modulus operator, and if condition checks for the odd number condition to successfully print them.

System.out.print displays in a single line, whereas System.out.println displays into a new line after each iteration.

Below is the code to Display odd numbers between 1 to 99:

public class Main {
  public static void main(String[] args) {
  for (int i = 1; i < 100; i++) {
      if (i % 2 != 0) {
        System.out.print(i+" ");
      }
    }
    }
}

As we see that, after Successfully compilation of the above code, we get the output as 1 3 5 7 ….. till 99.

Below is the output of the above program:

Java Program to Print Odd Numbers From 1 to 99

You may also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *