Answer to Question #274815 in Java | JSP | JSF for Adeng

Question #274815

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


1
Expert's answer
2021-12-03T07:45:16-0500
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n1,n2,n3,n4,n5;
		int [] arr=new int[5];
		System.out.print("Enter the first number: ");
		n1=in.nextInt();
		arr[0]=n1;
		System.out.print("Enter the second number: ");
		n2=in.nextInt();
		arr[1]=n2;
		System.out.print("Enter the third number: ");
		n3=in.nextInt();
		arr[2]=n3;
		System.out.print("Enter the fourth number: ");
		n4=in.nextInt();
		arr[3]=n4;
		System.out.print("Enter the fifth number: ");
		n5=in.nextInt();
		arr[4]=n5;
		
		int n=5;
		
		System.out.println("\nChoose which operation you want to perform:\n1. Addition\n2. Subtraction\n");
		System.out.println("\nEnter the operation of odd: ");
		int c=in.nextInt();
		int sum1=0, sub1=0;
		if(c==1){
		    for(int i=0;i<n;i++){
		        if (arr[i]%2!=0){
		            sum1+=arr[i];
		        }
		    }
		    System.out.println("The sum of odd numbers is: "+sum1);
		}
		else if(c==2){
		   for(int i=0;i<n;i++){
		        if (arr[i]%2!=0){
		            sub1-=arr[i];
		        }
		    } 
		    System.out.println("The difference of odd numbers is: "+sub1);
		}
		System.out.println("\nEnter the operation of even: ");
		int c2=in.nextInt();
		int sum2=0, sub2=0;
		if(c2==1){
		    for(int i=0;i<n;i++){
		        if (arr[i]%2!=0){
		            sum2+=arr[i];
		        }
		    }
		    System.out.println("The sum of even numbers is: "+sum1);
		}
		else if(c2==2){
		   for(int i=0;i<n;i++){
		        if (arr[i]%2!=0){
		            sub2-=arr[i];
		        }
		    } 
		     System.out.println("The difference of even numbers is: "+sub2);
		}
		
	}
}


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