Answer to Question #271738 in Java | JSP | JSF for jade

Question #271738

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.



1
Expert's answer
2021-11-26T07:09:03-0500
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 + " ");
	   }
	}
   }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS