Google News
logo
Java Program to print Odd numbers from 1 to n or 1 to 100
In the following example we have provided the value of n as 100 so the program will print the odd numbers from 1 to 100.

The logic we are using in this program is that we are looping through integer values from 1 to n using for loop and we are checking each value whether the value%2 !=0 which means it is an odd number and we are displaying it.
Program :
import java.util.Scanner;

public class PrintOddNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = sc.nextInt();
        System.out.println("Odd numbers from 1 to " + n + " are:");
        for(int i=1; i<=n; i++){
            if(i%2 != 0){
                System.out.print(i + " ");
            }
        }
    }
}
Output :
Enter the value of n: 36
Odd numbers from 1 to 36 are:1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35