Skip to content

Check for Prime

Method-1

check_Prime.cpp
#include <bits/stdc++.h>
using namespace std;

string check_Prime(int n){
    for (int i = 2; i < n; i++) {
        if (n % i == 0) {
            return "false";
        }
    }
    return "true";
}

int main() {
    int n;
    cin >> n;
    cout << check_Prime(n);
    return 0;
}

Input: 36
Output: false

Method-2

check_Prime.cpp
#include <bits/stdc++.h>
using namespace std;

string check_Prime(int n){
    for (int i = 2; i < sqrt(n); i++) {
        if (n % i == 0) {
            return "false";
        }
    }
    return "true";
}

int main() {
    int n;
    cin >> n;
    cout << check_Prime(n);
    return 0;
}

Input: 36
Output: false