Delete prime numbers from an array
C program to delete prime numbers from an array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
void print_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:%d\n",i+1,a[i]);
}
}
int prime(int num)
{
int i, flag;
flag=0;
for(i=2;i<num/2;i++)
{
if(num%i == 0)
{
flag = 1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
void delete(int a[MAX], int n)
{
int i, j;
for(i=0;i<n;i++)
{
if(prime(a[i]))
{
for(j=i;j<n;j++)
{
a[j] = a[j+1];
}
n=n-1;
//decrease loop counter by 1, to check shifted element
i--;
}
}
printf("\nModified array is\n");
print_array(a, n);
}
void main()
{
int arr[MAX];
int i, n;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
delete(arr, n);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :23
Element 3 :25
Element 4 :56
Element 5 :45
Element 6 :11
Element 7 :7
Element 8 :58
Modified array is
Element 1 :12
Element 2 :25
Element 3 :56
Element 4 :45
Element 5 :58