You are on page 1of 9

#include

#include <iostream>
using namespace
std;

// create a class
class IsPrime {
// private data
members
private:
int number;

// a public function
with a
// int type
parameter
public:
// getNumber()
function to insert
// the number
void getNumber() {
cout << "Enter
Number : ";
cin >> number;
}

// isprime()
function to check if
the
// number is prime
or not
void isprime() {
// initilaize two int
type variable
int index, check =
0;

// for loop to
check whether the
number
// is prime or not
for (index = 2;
index < number;
index++) {
// if condition to
check if the number
is
// divisible by the
index
if (number %
index == 0) {
// print not
prime if condition
satisfied
cout << number
<< " is not Prime." <<
endl;
// increases
check counter
check++;

// break to end
the loop
break;
}
}

// if condition to
check counter if
// count = 0 then it
will print prime
if (check == 0) {
cout << number
<< " is Prime." <<
endl;
}
}
};

int main() {
// create an object
IsPrime P;

// calling
getNumber()
function to
// insert the
number
P.getNumber();

// calling isprime()
function to check if
// the number is
prime or not
P.isprime();

return 0;
}

You might also like