[ Write a Program to input 10 numbers into an integer array and display the sum of prime numbers only.
Program Code :
import java.util.*;
class Prime_in_Array
{
public static void main(String args[])
{
int i,j,c=0,ps=0;
int n[]=new int[10];
Scanner sc=new Scanner(System.in);
//Accepting 10 Array Element
for (i=0;i<10;i++)
{
System.out.print("Enter the Array Element = ");
n[i]=sc.nextInt();
}
System.out.println("Prime Array Elements are:-");
for (i=0;i<10;i++)
{
c=0;
//Checking Prime Number from Array Elements
for(j=1;j<=n[i];j++)
if(n[i]%j==0)
c++;
if(c==2)
{
System.out.println(n[i]);
ps=ps+n[i];//Sum of Prime Number from Array
}
}
System.out.println("Sum of Prime No = "+ps);
}
}
No comments