Have the user input 5 numbers and outputs witch numbers are prime
#include<iostream>
using namespace std;
bool checkPrimeNumber(int n) {
bool isPrime = true;
if (n == 0 || n == 1) {
isPrime = false;
}
else {
for (int i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
int main(){
cout<<"Enter five numbers:\n";
int arr[5];
for(int i=0; i<5; i++){
cin>>arr[i];
}
cout<<"Prime numbers are:\n";
for(int i=0; i<5; i++){
if(checkPrimeNumber(arr[i])){
cout<<arr[i]<<" ";
}
else{
cout<<" ";
}
}
}
Comments
Leave a comment