Answer to Question #226609 in C++ for Johnnie

Question #226609
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-16T05:47:28-0400
#include <iostream>

using namespace std;

bool is_prime(int number) {
    if (number < 2) return false;
    if (number == 2) return true;
    if (number % 2 == 0) return false;
    for (int i = 3; (i * i) <= number; i += 2) {
        if (number % i == 0) return false;
    }
    return true;
}

int main() {
    int array[5];
    int num;
    cin >> num;
    if (is_prime(num)) {
        array[2] = num;
    }

    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
APPROVED BY CLIENTS