Answer to Question #272232 in Java | JSP | JSF for ilham

Question #272232

 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-27T11:37:29-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = in.nextInt();
        int count = 0;
        while (true) {
            if (number % 2 != 0) {
                System.out.println(number);
                count++;
            }
            if (number == Integer.MIN_VALUE) {
                break;
            }
            number--;
        }
        System.out.println("The number of odd numbers: " + count);

    }
}

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