adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C++ To check a given number is prime or not using Class with constructor and destructor

Program in C++ To check a given number is prime or not using Class

#include<iostream>
#include<conio.h>
 using namespace std;
  class prime
   {
       int n;
       int isPrime(int n);
    public:
     prime()  //Default Constructor
     {
         n=0;
     }
     prime(prime &x)  //Copy Constructor
      { 
          n=x.n;
      }
     ~prime(){}   //Destructor 
     void input()
     {
         cout<<"Input a number:-";
         cin>>n;
     }
     void show()
     {
        int r=isPrime(n);
        if(r==0)
            cout<<n<<" is prime No.";
        else
            cout<<n<<" is not prime No.";
     }
   };
 int prime::isPrime(int n)
   {
       int p=0,i;
       for(i=2;i<n/2;i++)
        {
         if(n%i==0)
          {
           p=1;
           break;
    }
  }
  return p;
   }
 int main()
 {
     prime p1;
     p1.input();
     p1.show();
     getch();
 }
Output:-











Related Programs

  1. Program in C++ to find greatest prime number from array
  2. Program in C++ to find greatest prime number from array Using function
  3. Program in C++ to find greatest prime number from array Using class
  4. Program in C++ to find smallest prime number from array
  5. Program in C++ to find smallest prime number from array Using function
  6. Program in C++ to find smallest prime number from array Using class

Post a Comment

1 Comments

Anonymous said…
great blog thanks !
cheek Prime Check for more info