Write a Java program that asks the user to enter an integer number N and successively display all the odd numbers that are less than or equal to N.
Improve the program so that it also displays the number of odd numbers
less than or equal to N.
import java.util.Scanner;
class JavaExample {
public static void main(String args[]) {
int n = (new Scanner(System.in)).nextInt();
System.out.print("Odd Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
Comments
Leave a comment