Prime number program in C++

Today we shall learn how to write a program to find a number is Prime number or not.Before we start writing a program we should learn the basic definition.A number is said to be prime number if”It is divisible by one and itself only”.Therefore while deciding a prime number we should about the factors of the given number.

To find the factors of the given number we should divide the number.If the remainder is 0 then number is divisible and it is said to be the factor of the given number.Here we shall use the modulous operator(%) to find the remainder.Let us write the code:

class prime_number
  int n;
  void calculating();
void prime_number :: calculating()

    int flag=0,count=0;
    for(int i=2;i<n;i++)
       if(count !=0)
	cout<<"\nGiven number is divisible by"<<i;
    if(flag ==0)
     cout<<"\n******Given number "<<n<<" is a prime number*******";
    cout<<"\n********There fore given number "<<n<<" is not a prime number*********";
void main()
  prime_number ob;
  cout<<"Enter the number to check for prime number:";

You can download the code:download


About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my Master's in Computer Science.
This entry was posted in C++ and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s