Programming Examples
Cpp program to find prime numbers in an array
Write a Cpp program to find the prime numbers in an array.
Solution
#include<iostream>
using namespace std;
int main(){
int num[10],a,b;
cout<<"Enter 10 positive integer numbers"<<endl;
for(a=0;a<10;a++)
{
cin>>num[a];
}
cout<<"Prime numbers are: ";
for(a=0;a<10;a++)
{
bool isPrime=true;
if(num[a]==1)
{
isPrime=false;
}
for(b=2;b<num[a];b++)
{
if(num[a]%b==0)
{
isPrime=false;
break;
}
}
if(isPrime)
{
cout<<num[a]<<" ";
}
}
return 0;
}
Output
Enter 10 positive integer numbers
12
1
13
7
25
67
89
11
45
76
Prime numbers are: 13 7 67 89 11Â