Computer Science, asked by irenerebeccamaria72, 4 months ago

Using switch-case write a menu-driven program to perform the following :

If choice=1, accept values into an integer array of size 10, and find the sum of all the even numbers in it.  

If choice=2, accept 15 values into an integer array and find the sum of all the prime numbers in it.

Else display ‘Wrong choice’.​

Answers

Answered by BrainlyProgrammer
5

Code:-

package Coder;

import java.util.*;

public class SwitchArray {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("1.Sum of even elements in array\n2.Sum of prime elements in array\nEnter your choice:-");

int choice=sc.nextInt();

int a[],i,s=0,c=0;

switch(choice)

{

case 1:

a=new int[10];

System.out.println("Emter elements in the array");

for(i=0;i<10;i++)

{

a[i]=sc.nextInt();

}

for(i=0;i<10;i++)

{

if(a[i]%2==0)

s+=a[i];

}

System.out.println("Sum of even elements="+s);

break;

case 2:

a=new int[15];

System.out.println("Emter elements in the array");

for(i=0;i<10;i++)

{

a[i]=sc.nextInt();

}

for(i=0;i<10;i++)

{

for(int j=1;j<=a[i];j++)

{

if(a[i]%i==0)

c++;

}

if(c==2)

s+=a[i];

}

System.out.println("Sum of prime elements="+s);

break;

default:System.out.println("Wrong Choice");

}

}

}

Variable Description:-

  1. a:- array to accept numbers as input from the user
  2. c:- counter variable to check if the number is prime
  3. i:- loop variable
  4. j:- loop variable
  5. s:- to calculate sum of elements
Similar questions
Math, 2 months ago