C++ Program to check a number is prime or not using class and object


In this program, You will learn how to check a number is prime or not using class and object in C++.


Some list of prime number's: 2, 3, 5, 7, 11, 13, 17


Example: How to check a number is prime or not using class and object in C++

Run it Now
#include<iostream>
using namespace std;

class Test {
public:

    int check(int x) {
        int i, p = 0;

        for (i = 2; i < x; i++) {
            if (x % i == 0) {
                p = 1;
                break;
            }
        }
        return p;
    }
};

int main() {

    int x, p;

    cout << "Enter a Number :";
    cin >> x;

    Test obj;
    p = obj.check(x);

    if (p == 0) {
        cout << "\nNumber is Prime :" << x;
    } else {
        cout << "\nNumber is Not Prime :" << x;
    }

    return 0;
}

Output:

Enter a Number :13
Number is Prime :13