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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int one = in.nextInt();
int two = in.nextInt();
int sum = 0;
int product = 1;
StringBuilder oddNumbers = new StringBuilder();
System.out.print("Even numbers: ");
for (int i = Math.min(one, two); i <= Math.max(one, two); i++) {
if (i % 2 == 0) {
sum += i;
System.out.print(i + " ");
} else {
product *= i;
oddNumbers.append(i).append(" ");
}
}
System.out.println("\nOdd numbers: " + oddNumbers);
System.out.println("Sum of even numbers = " + sum);
System.out.println("Product of odd numbers = " + product);
}
}
Comments
Leave a comment