Answer to Question #324148 in Java | JSP | JSF for Chiarra

Question #324148

Against All "Odds"

Instructions:

  1. Input a positive integer. This will serve as the starting point of the loop.
  2. Using a while() loop, print out all the odd numbers starting from the inputted integer, until 0. The outputted numbers must all be separated by line.
  3. Also remember that since the loop goes to descending order, a decrement variable shall be created and decreased per iteration for the loop to terminate when it reaches 0.

Instructions

  1. Scan an integer that will accept an integer greater than 0 and store it in a variable.
  2. Using a while() loop, print out all the odd numbers starting from the inputted integer, until 0. The outputted numbers must all be separated by line, just like that of the sample output.
  3. Also remember that since the loop goes to descending order, a decrement variable shall be created and decreased per iteration for the loop to terminate when it reaches 0.

Input

A line containing an integer.

10

Output

Multiple lines containing an integer.

9
7
5
3
1






1
Expert's answer
2022-04-05T13:18:01-0400
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int num = in.nextInt();
        while(num > 0){
            if(num%2 != 0){
                System.out.println(num);
            }
            num--;
        }
    }
}

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