Answer to Question #268977 in Java | JSP | JSF for mk27

Question #268977
  1. LOOPS&CONDITIONS: Create a program that takes in two numbers and then calculate the sum of all even numbers in between the two numbers. The program should also calculate the products of all the odd numbers between the provided numbers.

 

Sample run 1:                                                 

Enter two numbers: 2 8                                                

Output:

Even numbers: 2 4 6 8

Odd numbers: 3 5 7

Sum of even numbers = 20

Product of odd numbers = 105

1
Expert's answer
2021-11-19T15:32:23-0500
import java.util.Scanner;


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter two numbers: ");
		int minNumber = keyBoard.nextInt();
		int maxNumber = keyBoard.nextInt();
		int sumEvenNumbers = 0;
		int productOddNumbers = 1;
		System.out.print("Even numbers: ");
		for (int i = minNumber; i <= maxNumber; i++) {
			if (i % 2 == 0) {
				System.out.print(i + " ");
				sumEvenNumbers += i;
			}
		}
		System.out.print("\nOdd numbers: ");
		for (int i = minNumber; i <= maxNumber; i++) {
			if (i % 2 != 0) {
				System.out.print(i + " ");
				productOddNumbers *= i;
			}
		}
		System.out.println("\nSum of even numbers = " + sumEvenNumbers);
		System.out.println("Product of odd numbers = " + productOddNumbers);
		keyBoard.close();


	}


}

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