Pages

100 Contemporary Living Room Designs

Tuesday, September 4, 2012

C++ PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT USING PARAMETERISED CONSTRUCTOR.

#include<iostream.h>
#include<conio.h>
class prime
{
   int a,k,i;
   public:
   prime(int x)
   {
     a=x;
   }
   void calc();
   void print();
};
void prime::calc()
{
    k=1;
    for(i=2;i<a/2;i++)
    {
       if(a%i==0)
       {
          k=0;
          break;
       }
       else
          k=1;
     }
}
void prime::print()
{
    if(k==1)
       cout<<"\nPrime Number";
    else
       cout<<"\nNot Prime";
}
void main()
{
    int n;
    clrscr();
    cout<<"\nEnter the number:\n";
    cin>>n;
    prime e(n); // parameterised constructor call
    e.calc();
    e.print();
    getch();
}

output


1 comment:

  1. Prime number program in C++

    prime number is only divisible by 1 and itself,
    we can easily write prime number program in c++, just check number is not divisible by any other numbers except 1 and number itself.

    ReplyDelete