n
as 100
so the program will print the odd numbers from 1 to 100.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.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 + " ");
}
}
}
}
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