C++ Program to Check Prime Number By Creating a Function














































C++ Program to Check Prime Number By Creating a Function



DESCRIPTION:

 

This  program is written in C++ and checks if a given number is prime or not by using a function to perform the task.

 Here, the features of OOP have been used. A class with its member function and variable has been created to wrap up all the data in a single unit. To access the data members and functions, an object has been created of the same class.

An exception handling method has been used, namely, try-catch block to handle any invalid input such as negative integer.

 

ALGORITHM:

 

Input: An integer is accepted in constructor

Output: Shows if the integer is prime or not

 

STEPS:

1.     Create a class to store the integer and the member function to implement the task.

2.     Create the constructor to assign the member variable with the given integer and destructor to destroy the object after execution.

 

//Function to check for prime number begins

3.     In the member function, set flag=0

4.     Check if the given integer is valid or not. For the latter case, show appropriate message.

5.     If valid, then a loop is set to check if the integer is divisible by any other integer from 2 to half of itself.

6.     If found divisible, then increment the flag and break from the loop.

7.     Else the loop is terminated normally.

8.     Exiting the loop, check if flag=0, then print %u201CIt a is prime number.%u201D

9.     Else, Print %u201CIt is not a prime number%u201D.

//Function to check prime number ends

 

 CODE:

 

#include<iostream>
using namespace std;

class Prime
{
	private:
		int number;

	public:

		Prime()//constructor
		{
			cout<<endl<<"Enter the number to be checked:   ";
			cin>>number;	
		}
		~Prime()//destructor to destroy the object after execution
		{
			cout<<endl<<endl<<"The program is closing."<<endl<<"The object has been destroyed.";
		}

		void checkPrime()//function to check for the prime number
		{
			int flag,i;
			flag=0;
			try //exception handling block 
			{
				if(number<=0)//when invalid input is given
				{
					throw(number);
				}
				else
				{
					for(i=2;i<=number/2;i++)//loop to check for the divisibility
					{

	 					if(number%i==0)
	 					{

	 						flag++;
	 						break;
		 				}	
					}

					if(flag==0)
					cout<<endl<<"The given number  "<<number<<"  is prime.";
					else
					cout<<endl<<"The given number   "<<number<<"  is not prime.";
				}	
			}

			catch(int x)//show appropriate message in invalid input case
			{
				cout<<endl<<"Invalid number, "<<x<<" is given.";
			}
		}			
};


int main()
{
	Prime obj;
	obj.checkPrime();
	return 0;
}

 

 INPUT AND OUTPUT:

 

SET 1:

Enter the number to be checked:   167

The given number  167  is prime.


The program is closing.

The object has been destroyed.


SET 2:

Enter the number to be checked:   -57

Invalid number, -57 is given.


The program is closing.

The object has been destroyed.

 

SET 3:

Enter the number to be checked:   696

The given number   696  is not prime.


The program is closing.

The object has been destroyed.



Comments