Answer to Question #226614 in C++ for Kvngdanzy

Question #226614
One dimensional array that accepts input from the keyboard and test for prime , if prime inputs it into the 3rd index of the array
1
Expert's answer
2021-08-16T14:49:52-0400
#include <iostream>
#include <string>
using namespace std;


bool isPrime(int n);


int main()
{
	int numbers[5];
	for(int i=0;i<5;i++){
		cout<<"Enter number "<<(i+1)<<": ";
		cin>>numbers[i];
		if(isPrime(numbers[i])){
			numbers[2]=numbers[i];
		}
	}
	if(isPrime(numbers[2])){
		cout<<"The prime number is: "<<numbers[2]<<"\n";
	}


	cin>>numbers[0];
	return 0;
}


bool isPrime(int n){
	if (n == 0 || n == 1) {
		return false;
	}else {
		for (int i = 2; i <= n / 2; ++i) {
			if (n % i == 0) {
				return false;
			}
		}
	}
	return true;
}

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