Write a function to find whether a given no. is prime or not. Use the same to generate7the prime numbers less than 100.



#include<iostream.h>
#include<conio.h>
class prime
{
public:
int  p(int n);
};

int prime::p(int n)
{
                for(int i=2;i<=n/2;i++)
                {
                 if(n%i==0)
                 {
                 return 0;
                 }
                }
                return 1;
}
void main()
{
 clrscr();
 int n;
 cout<<"enter the number which has to be checked whether prime or not"<<endl;
 cin>>n;
 prime obj;
                if(obj.p(n))
                cout<<"the number entered is prime "<<endl;
                else
                cout<<"the number entered is not prime"<<endl;
                cout<<"enter 1 to see prime number less than 100, else enter 0"<<endl;
       cin>>n;
       if(n)
       {
                cout<<"the prime numbers less than 100 are";
 for(int i=2;i<=100;i++)
 {
 if(obj.p(i)==1)
 cout<<i<<endl;
 }
 }
 getch();
}


Output:-




Comments