Answer to Question #273728 in C++ for DANISH

Question #273728

BY LOOPS




Write a program that ask the user to type a value and check whether it is prime or not.





1
Expert's answer
2021-11-30T18:17:56-0500
#include <iostream>

int main() {
    int x;
    std::cout << "Enter the integer: ";
    std::cin >> x;
    bool found = false;
    for (long long i = 2LL; i * i <= (long long)x && !found; i++) {
        found = x % i == 0;
    }
    std::cout << (found? "It's not a prime!\n" : "It's a prime number\n");
    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