Q:

C program to delete prime numbers from an array

0

C program to delete prime numbers from an array

Given an array of integer elements and we have to remove prime numbers using C program.

Example:

    Input:
    Array elements are: 
    100, 200, 31, 13, 97, 10, 20, 11

    Output:
    Array elements after removing prime numbers: 
    100
    200
     10
     20

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to remove prime numbers from an array using C program

#include <stdio.h>

//function to check number is prime or not
//function will return 1 if number is prime
int isPrime(int num)
{
	int i; //loop counter
	//it will be 1 when number is not prime
	int flag=0; 
	//loop to check number is prime or not
	//we will check, if number is divisible
	//by any number from 2 to num/2, then it
	//will not be prime
	for(i=2; i<num/2; i++)
	{
		if(num%i ==0)
		{
			flag =1;
			break;
		}
	}
	//flag is 1, if number is not prime
	if(flag==1)
		return 0;
	else
		return 1;
}

int main()
{
	int i,j; //loop counters
	//declaring array with prime and not prime numbers
	int arr[]={100, 200, 31, 13, 97, 10, 20, 11};
	//calculate length of the array
	int len = sizeof(arr)/sizeof(arr[0]);
	
	//delete prime numbers
	for(i=0; i<len; i++)
	{
		if(isPrime(arr[i]))
		{
			//number is prime, then shift other
			//elements to the left
			for(j=i; j<len; j++)
			{
				arr[j] = arr[j+1];
			}
			//decrease loop counter by 1,
			//to check shifted element
			i--;
			//decrease the length
			len--;			
		}
	}
	
	//print elements after removing prime numbers
	printf("Array elements after removing prime numbers:\n");
	for(i=0; i<len; i++)
		printf("%3d\n",arr[i]);
	
	printf("\n");
	
	return 0;	
}

Output

Array elements after removing prime numbers: 
100
200
 10
 20

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to check prime numbers in an array... >>
<< C program to calculate median of an array...