Answer to Question #226607 in C++ for Johnnie

Question #226607
Write a C++ program that creates a one dimensional array with of length 5 elements. Your
program should read an integer from the keyboard and test if it is prime. If the integer is
prime, the program should insert the integer into the 3rd index of the array�
1
Expert's answer
2021-08-16T14:49:57-0400
#include <iostream>
#include <string>






using namespace std;




//This function checks if the number is prime
bool isPrime(int number){
	if (number == 0 || number == 1) {
		return false;
	}else {
		for (int i = 2; i <= number / 2; ++i) {
			if (number % i == 0) {
				return false;
			}
		}
	}
	return true;
}
int main()
{
	int elements[5];


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


	int k;
	cin>>k;
	return 0;
}





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