using SWITCH selection statement
Write a Program that will ask the user to accept 5
integers, select an arithmetic operation for odd
and even, and perform the corresponding
operation.
Enter the first number: 34
Enter the second number: 25
Enter the third number: 62
Enter the fourth number: 13
Enter the fifth number: 27
Choose which operation you want to perform:
1. Addition
2. Subtraction
Enter the operation of odd: 1
Enter the operation of even: 2
The sum of odd numbers is: 65
The difference of even numbers is: -28
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);
int numbers[] = new int[5];
System.out.print("Enter the first number: ");
numbers[0] = keyBoard.nextInt();
System.out.print("Enter the second number: ");
numbers[1] = keyBoard.nextInt();
System.out.print("Enter the third number: ");
numbers[2] = keyBoard.nextInt();
System.out.print("Enter the fourth number: ");
numbers[3] = keyBoard.nextInt();
System.out.print("Enter the fifth number: ");
numbers[4] = keyBoard.nextInt();
System.out.println("\n\nChoose which operation you want to perform:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.print("Enter the operation of odd: ");
int operationOdd = keyBoard.nextInt();
System.out.print("Enter the operation of even: ");
int operationEven = keyBoard.nextInt();
switch (operationOdd) {
case 1: {
int sumNumbers = 0;
for (int i = 0; i < numbers.length; i++) {
int modResult = numbers[i] % 2;
switch (modResult) {
case 1:
sumNumbers += numbers[i];
break;
}
}
System.out.println("The sum of odd numbers is: " + sumNumbers);
}
break;
case 2: {
int differenceNumbers = numbers[0];
for (int i = 1; i < numbers.length; i++) {
int modResult = numbers[i] % 2;
switch (modResult) {
case 1:
differenceNumbers -= numbers[i];
break;
}
}
System.out.println("The difference of odd numbers is: " + differenceNumbers);
}
break;
default:
break;
}
switch (operationEven) {
case 1: {
int sumNumbers = 0;
for (int i = 0; i < numbers.length; i++) {
int modResult = numbers[i] % 2;
switch (modResult) {
case 0:
sumNumbers += numbers[i];
break;
}
}
System.out.println("The sum of even numbers is: " + sumNumbers);
}
break;
case 2: {
int differenceNumbers = numbers[0];
for (int i = 1; i < numbers.length; i++) {
int modResult = numbers[i] % 2;
switch (modResult) {
case 0:
differenceNumbers -= numbers[i];
break;
}
}
System.out.println("The difference of even numbers is: " + differenceNumbers);
}
break;
default:
break;
}
keyBoard.close();
}
}
Comments
LOL THANKS!
Leave a comment