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 

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz