Answer to Question #255496 in C++ for Ahmad aref

Question #255496

Have the user input 5 numbers and outputs witch numbers are prime


1
Expert's answer
2021-10-23T08:06:06-0400
#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<<" ";
		}
	}
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS