C program to find all prime numbers from array

Program in the C programming language to find all the prime numbers from the inputted array. Below is the C program –

/* C program to find all prime numbers from the inputted array */
#include<stdio.h>
#include<conio.h>
void main()
{
     int ar[100],i,n,j,counter;
     
     printf("Enter the size of the array ");
     scanf("%d",&n);
     printf("\n Now enter the elements of the array");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     
     printf(" Array is -");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     
     printf("\n All the prime numbers in the array are -");
     for(i=0;i<n;i++)
     {
           counter=0;
           for(j=2;j<ar[i];j++)
           {
                 if(ar[i]%j==0)
                 {
                       counter=1;
                       break;
                 }
           }
           if(counter==0)
           {
                 printf("\t %d",ar[i]);
           }
     }
     getch();
}

Output –
Enter the size of the array – 5
Now enter the elements of the array – 23      98      45      101      6
Array is – 23      98      45      101      6
All the prime numbers in the array are – 23      101

yashan has written 70 articles

15 thoughts on “C program to find all prime numbers from array

    1. vanipriya says:

      in if condition we check “==0” that mean here we use comparision operator
      instead of “==0″ this when we use”=0″ this
      example:
      if(a==0)
      it check condition
      if(a=0)
      it mean a value became 0
      it gives error so we use”==0” in checking conditions.

Cancel reply

Leave a Reply to swr

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>