Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that has array.From that array extract prime number and remove from the array and display the new array. I am trying but getting the same answer i.e values of array...please help
______________________________________________________________________________
code :
=============================================================
import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int i,j,size;
boolean status;
System.out.print("Enter size of array=");
size=s.nextInt();
int arr[]=new int[size];
System.out.println("Enter Elements in array...");
for(i=0;i<arr.length;i++)>
{
arr[i]=s.nextInt();
}
for( i=0;i<arr.length;i++)>
{
status=true;
for(j=2;j<arr[i];j++)>
{
if(arr[i]%j==0)
{
status=false;
break;
}
}
if(status)
{
System.out.println("Prime Number Found="+arr[i]);
}
}

}
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
suppose i enter 5 elements : 5,2,10,8,3
answer=5,2,3 are prime Numbers

But 5,2,3 should be deleted and new array should be displayed like 10,8
Posted
Updated 4-Oct-14 3:07am
v4
Comments
CPallini 3-Oct-14 9:17am    
2 is a prime number, as far as I know.
A94 3-Oct-14 23:32pm    
yes 2,5,7,11 and so on are prime numbers but how to remove it from array..
Richard MacCutchan 4-Oct-14 9:22am    
There are plenty of examples to be found on how to identify prime numbers. You would be better using a collection class which allows for easy removal of items .

1 solution

Why not try a little search with the keywords 'how to find prime numbers java'.

To make it easy to delete values form any position in an array, you should look into using dynamic arrays such as linked lists.

If this is school work, you might need to create your own:
Linked Lists[^]

You can aslo look into Java collections:
Java Collections Tutorial[^]
 
Share this answer
 
v2
Comments
A94 3-Oct-14 23:33pm    
Hey..! is Maths required for software development and if required what type of maths is required for ex: calculus,discrete or algebra please tell...
George Jonsson 4-Oct-14 5:04am    
Math is involved in a lot of software, usually calculus, geometry and algebra, like equation solving and matrix calculations.
Also statistics and probability is good to know.
A94 4-Oct-14 9:05am    
OK! I have updated the program and sought out prime numbers from array but how to delete them and display new array please help....
George Jonsson 4-Oct-14 22:46pm    
See my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900