Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to remove prime numbers from array and display the new array ? I have already identified prime numbers but cannot remove correctly.

*> Code
============================================================================
C#
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];
        int tmp[]=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;
                    tmp[i]=arr[i];
                    break;
                }
            }
            if(status==true)
            {
                System.out.println("Prime Number Found="+arr[i]);
            }
        }
        System.out.println();
        System.out.println("New Array....");
        for(i=0;i<tmp.length;i++)
        {
            System.out.println(tmp[i]);
        }
    }
}


==================================================================
Output : Suppose I enter 5 elements 2,4,6,7,12

I get 2,7 as prime numbers

now new array will be displayed like 0,4,6,0,12
but i want to display like 4,6,12 only and is there anyway by which i can use only one array to perform this operation.

Thank you in advance...
Posted
Comments
Mehdi Gholam 6-Oct-14 6:40am    
Just use a java List which has add() and remove().
Maciej Los 6-Oct-14 7:31am    
The only way to remove prime numbers from array is to:
- create another array to store non-prime numbers
or
- move non-prime numbers at the end of array and then decrease the size of array.

As Mehdi Gholam suggested, use ArrayList[^] instead.

Please, see:
http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array[^]
AP900 6-Oct-14 11:30am    
Thank you for the solution.Can you give tips to become good and through at programming skills and which language is better java or c#..please suggest..

1 solution

You can sort out those unwanted values by just discarding them:

Java
// last for loop
for(i=0;i<tmp.length;i++)
{
    if(tmp[i] != 0 )
    {
        System.out.println(tmp[i]);
    }
}


Best otpion would be to set those non valid values to a value of -1, which is regarded as an invalid as it is a negative natural number.
The for loop would then lok like that:

Java
// last for loop
for(i=0;i<tmp.length;i++)
{
    if(tmp[i] >= 0 ) // only let numbers in bigger than 0
    {
        System.out.println(tmp[i]);
    }
}
 
Share this answer
 
v2

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