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
- Program in C++ to find greatest prime number from array
- Program in C++ to find greatest prime number from array Using function
- Program in C++ to find greatest prime number from array Using class
- Program in C++ to find smallest prime number from array
- Program in C++ to find smallest prime number from array Using function
- Program in C++ to find smallest prime number from array Using class
1 Comments
cheek Prime Check for more info