Computer Science, asked by Nightmare8063, 1 month ago

WAP to take 20 integers elements in an array and find the sum of the prime elements

Answers

Answered by mohammadadnan7
1

Answer:

import java.util.*;

class Sum{

public static void main(String args[]){

Scanner sc=new Scanner(System.in);

int arr[]=new int[20];

int c=0,i,j,sum=0;

System.out.println("enter the 20 integers");

for(i=0;i<20;i++){

arr[i]=sc.nextInt();

} // end of loop

for(j=1;j<=arr[i];j++){

if(j%arr[i]==0){

c++;

} //end of if

if(c==2){

sum=sum+arr[i];

} // end of if

} //end of for loop

System.out.println(sum);

} //end of main

} //end of class

Explanation:

c==2 is taken for the prime number condition

c is taken for counting the factors of arr[i]

hope this will help you

Similar questions