CPP program to check a number is prime or not using class and object


 /*
CPP program to check a number is prime or not using class and object
2, 3, 5, 7, .......
*/
#include<iostream>
using namespace std;
class prime{
private:
        int n, isPrime;//Data Members
public:
        prime(int num){//Constructor
                n = num;
        }
        void primeCk(){//Prime checking function
                isPrime = 1;
                if(n <= 1){
                        isPrime = 0;
                }
                for(int i = 2; i < n; i++){//10
                        if(n % i == 0){
                                isPrime = 0;
                                break;
                        }
                }
        }
        void display(){//Display function
                if(isPrime == 1){
                        cout<<"It's Prime"<<endl;
                }else{
                        cout<<"It's Not Prime"<<endl;
                }
        }
};
int main()
{
        prime obj(1);
        obj.primeCk();
        obj.display();
        return 0;
}



No comments:

Post a Comment