Odd number

Opposite of even numbers, odd numbers are having a difference of 3 unit or number. In other words, if the number is not completely divisible by 2 then it is an odd number.

Logic

The logic for printing odd numbers are pretty simple and straight forward, we only need to check if the number is not divisible by 2. If the number is not divisible by 2, then we have to print it.

Program

import java.util.Scanner;

public class OddNumbers {
    
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Take input from user.
        System.out.print("Print all odd numbers till : ");
        int n = scanner.nextInt();

        System.out.println("\nOdd numbers from 1 to " + n + " are : ");

        for(int i = 1; i <= n; i++) {

            // Check for odd or not.
            if((i % 2) != 0) {

                System.out.print(i + " ");

            }
        }
    }

}

Output

Print all odd numbers till : 50
Odd numbers from 1 to 50 are :
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