Answer to Question #241322 in C++ for SCADA HMI

Question #241322
Write an efficient program to determine if a positive integer, N, is prime.
and In terms of N, analyze the worst-case running time of your program?
1
Expert's answer
2021-09-23T17:45:50-0400
#include <iostream>

bool is_prime(const int prime){
    if (prime <= 0)
        return false;
    for(int i = 2; i <= prime / 2; i++)
        if(prime % i == 0)
            return false;
    return true;
}

int main() {
    int prime;
    std::cout << "Enter any positive number: ";
    std::cin >> prime;
    if (is_prime(prime))
        std::cout << prime << " is a prime number" << std::endl;
    else
        std::cout << prime << " is not a prime number" << std::endl;
    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