Program to find all prime numbers from array

2 years ago Admin 0

Find all prime numbers from array

This article illustrates how to find all prime numbers from array in c/c++.

find all prime numbers from array

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],size,i,j,n;
clrscr();
printf("Enter number of elements you want to enter in array:- ");
scanf("%d",&size);
printf("Enter elements in array:- ");
for(i=0;i<size;i++)
 {
  scanf("%d",&arr[i]);
 }
 printf("Prime numbers are :- \n");
 for(i=0;i<size;i++)
  {
	  n=0;
	  for(j=2;j<arr[i];j++)
	  {
	   if(arr[i]%j==0)
	    {
	     n++;
	     break;
	    }
	  }
	  if(n==0)
	   {
	    printf("%d \n",arr[i]);
	   }
  }
getch();
}

After compiling and executing the above C program output is:-

find all prime numbers from array


C++ Programming language

Program


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[20],size,i,j,n;
cout<<"Enter number of elements you want to enter in array:- ";
cin>>size;
cout<<"Enter elements in array:- ";
for(i=0;i<size;i++)
 {
  cin>>arr[i];
 }
 cout<<"Prime numbers are :- "<<endl;
 for(i=0;i<size;i++)
  {
	  n=0;
	  for(j=2;j<arr[i];j++)
	  {
	   if(arr[i]%j==0)
	    {
	     n++;
	     break;
	    }
	  }
	  if(n==0)
	   {
	    cout<<arr[i]<<endl;
	   }
  }
getch();
}

After compiling and executing the above C++ program output is:-

find all prime numbers from array


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to insert an element in an array
Program to search an element in an array
Program to find smallest element in the array
Program to sort an array in ascending order
rogram to calculate the sum and average of n number


want to know what are prime numbers ?

Click here


About article

Written By:- Admin

Published on:- 27/10/2017

Published By:- Snapcout


Don't forget to share the article if you like it:-