Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This code found the prime number in array and delete it;
- #include<stdio.h>
- #include<malloc.h>
- int n; //taking n as global variables because it is being used by shrink fun as well and its value is changing in both mains and shrink function.
- int isprime(int n)
- { int i,flag=0;
- for(i=2;i<=n/2;i++)
- { if(n%i==0)
- { flag++;
- break;
- }
- }
- if(flag==0 && n!=1)
- return 1;
- else
- return 0;
- }
- void shrink_arr(int *arr, int j)
- { printf("\n%d",j);
- if(j==n-1)
- arr[j]=4;//replacing last element with a non-prime number to stop the while loop in main function
- else
- { while(j<n-1)//keep shrinking till the last element
- /*'j' can maximum travel up to the 2nd last element so that j+1 become the last one*/
- {arr[j]=arr[j+1];
- j++;
- }
- }
- /*Since each time the shrink fun called, the array shrink by 1 element so n--;*/
- n--;
- return;
- }
- int main()
- {
- int *arr,i,j;
- printf("Enter number of elements:. ");
- scanf("%d",&n);
- arr= calloc(n,sizeof(int));
- //Enter all (distinct) elements of array/
- for(i=0;i<n;i++)
- { printf("Enter element %d: ",i+1);
- scanf("%d",&arr[i]);
- }
- printf("\n Here is the list:\n");
- for(i=0;i<n;i++)
- printf("%d ",arr[i]);
- for(i=0;i<n;i++)
- { while(isprime(arr[i])==1)
- shrink_arr(arr,i);
- }
- printf("\n List without prime number:\n");
- if (n==0)
- printf("\nList is empty:");
- /*Since if 'n' = 0 then below for loop will not excecute hence no need for else statement*/
- for(i=0;i<n;i++)
- printf("%d ",arr[i]);
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement