Thursday, 23 June 2011

A progarm to read an integer number and test if it is prime it uses a constructor with arguments

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<math.h>

struct anynumber
{
  int n;
  int prime;
  public :

   anynumber (int any)
   {
     prime = 1;
     n=any;
   }
   void isprime();
   void display()
   {
     if (prime==0)
       cout<<endl<<"Is not a prime number";
     else
       cout<<endl<<"Is a prime number";
   }
};
void anynumber :: isprime()
{
  int i;
  float s;
  s=sqrt(n);
  for (i=2; i<=s; i++)
  {
    if (n%i ==0)
    {
      prime =0;
      return;
    }
  }
}

main()
{
   int k;
   clrscr();
   cout<<"Enter the number"<<endl;
   cin>>k;
   anynumber testno(k);
   testno.isprime();
   testno.display();
   getch();
}

0 Comments:

 

blogger templates 3 columns | Make Money Online