To print odd numbers in Java, you can use a loop to iterate over a range of numbers and check if each number is odd using the modulo operator (%
).
Here is an example of how to print odd numbers in Java:
public class Main { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 2 != 0) { System.out.println(i); } } } }Source:wwtual.wturi.com
In this example, the for
loop iterates over the range of numbers from 1 to 10 and checks if each number is odd by checking if it is not divisible by 2 (i.e., its remainder when divided by 2 is not 0). If the number is odd, it is printed to the console using the println
method.
This code will output the following numbers:
1 3 5 7 9
You can modify this example to suit your specific needs, such as changing the range of numbers or using a different looping construct.