Answer to Question #225172 in C++ for Ulando

Question #225172

Write a function that determines whether an integer is a prime number.



1
Expert's answer
2021-08-11T03:22:03-0400
#include <iostream>
using namespace std;
bool is_prime(int a){
    if(a <= 1) return false;
    else if(a == 2) return true;
    else{
        for(int i = 2; i < a;i++){
            if(a % i == 0) return false;
        }
        return true;
    }
}
int main(){
    int input;
    cout<<"Input an integer: ";
    cin>>input;
    cout<<input<<" is";
    if(is_prime(input)) cout<<" prime.";
    else cout<<" not prime.";
    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