175k views
2 votes
Write a Java program named Problem 3 that prompts the user to enter two integers, a start value and end value ( you may assume that the start value is less than the end value). As output, the program is to display the odd values from the start value to the end value. For example, if the user enters 2 and 14, the output would be 3, 5, 7, 9, 11, 13 and if the user enters 14 and 3, the output would be 3, 5, 7, 9, 11, 13.

User Charlene
by
3.0k points

1 Answer

3 votes

Answer:

hope this helps

Step-by-step explanation:

import java.util.Scanner;

public class Problem3 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter start value: ");

int start = in.nextInt();

System.out.print("Enter end value: ");

int end = in.nextInt();

if (start > end) {

int temp = start;

start = end;

end = temp;

}

for (int i = start; i <= end; i++) {

if (i % 2 == 1) {

System.out.print(i);

if (i == end || i + 1 == end) {

System.out.println();

} else {

System.out.print(", ");

}

}

}

}

}

User Emanuella
by
3.3k points