Advertisement
Mr_kindle

Array2.c

Nov 8th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. //This code found the prime number in array and replace it with '0'
  2. #include<stdio.h>
  3. #include<malloc.h>
  4.  
  5. int isprime(int n)
  6. {   int i,flag=0;
  7.      for(i=2;i<=n/2;i++)
  8.         {    if(n%i==0)
  9.                 { flag++;
  10.                     break;
  11.             }
  12.         }
  13.         if(flag==0 && n!=1)
  14.         return 1;
  15.         else
  16.             return 0;
  17. }
  18.  
  19. int main()
  20. {
  21.    int *arr,i,j,n;
  22.      printf("Enter number of elements:. ");
  23.     scanf("%d",&n);
  24.     arr= calloc(n,sizeof(int));
  25.     //Enter all (distinct) elements of array/
  26.     for(i=0;i<n;i++)
  27.     {    printf("Enter element %d: ",i+1);
  28.         scanf("%d",&arr[i]);
  29.     }
  30.    
  31.     printf("\n Here is the list:\n");
  32.     for(i=0;i<n;i++)
  33.       printf("%d ",arr[i]);
  34.    
  35.     for(i=0;i<n;i++)
  36.     {    if(isprime(arr[i])==1)
  37.             arr[i]=0;
  38.     }
  39.     printf("\n List without prime number:\n");
  40.     for(i=0;i<n;i++)
  41.       printf("%d ",arr[i]);
  42.    
  43.    
  44.    
  45.    
  46.    
  47.     free(arr);
  48.    
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement