Site icon T4Tutorials.com

C++ Program with constructor destructor to Check Whether a Number can be Express as Sum of Two Prime Numbers

Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers using the constructor overloading and destructor.

Prime Number C++

 

Check-whether-a-number-can-be-express-as-sum-of-two-prime-numbers

C++ Program with constructor  to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
using namespace std;
class T4Tutorials
{
	protected :
		int n, i, num1,num2,num3,j;
	public :
		T4Tutorials()
		{
			num1=1,num2=1,num3=0;
			cout<<"Check Whether a Number can be Express as Sum of Two Prime Numbers:"<<endl;
			cout<<"----\n";
			cout<<"Input  a positive integer: "<<endl;
			cin>>n;
			  for(i=3; i<=n/2; i++)
		     {     
				/*prime number?*/
		      num1=1;
		      num2=1;
		      for(j=2; j<i; j++)
		       {     
		         if(i%j==0)
		           { num1=0;j=i;}
		        }
		      for(j=2; j<n-i; j++)
		       {     
		         if((n-i)%j==0)
		           { num2=0;j=n-i;}
		        }
		         if(num1==1 && num2==1)
		           { printf("%d =  %d + %d  \n",n,i,n-i); 
		           num3=1;}
		    }
         if(num3==0)
           {
				cout<<"can not be expressed as sum of two prime numbers."<<endl;
			}
		}
};
int main()
{
	T4Tutorials a;
}

Output

Check Whether a Number can be Express as Sum of Two Prime Numbers:input a positive integer:

5

can not be expressed as sum of two prime numbers.

C++ Program with destructor  to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
using namespace std;
class T4Tutorials
{
	protected :
		int n, i, num1,num2,num3,j;
	public :
		~T4Tutorials()
		{
			num1=1,num2=1,num3=0;
			cout<<"Input  a positive integer: ";
			cin>>n;
			  for(i=3; i<=n/2; i++)
		     {     
				/*check for prime number?*/
		      num1=1;
		      num2=1;
		      for(j=2; j<i; j++)
		       {     
		         if(i%j==0)
		           { num1=0;j=i;}
		        }
		      for(j=2; j<n-i; j++)
		       {     
		         if((n-i)%j==0)
		           { num2=0;j=n-i;}
		        }
		         if(num1==1 && num2==1)
		           { printf("%d =  %d + %d  \n",n,i,n-i); 
		           num3=1;}
		    }
         if(num3==0)
           {
				cout<<"\n%d can not be expressed as sum of two prime numbers.\n\n";
			}
		}
};
int main()
{
	T4Tutorials a;
}

C++ Program with constructor overloading to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include<iostream>
using namespace std;
class T4Tutorials
{
	protected :
		int n, i, num1,num2,num3,j;
		int flg5,flg6,flg4;
	public :
		T4Tutorials (int one)
		{
			num1=1,num2=1,num3=0;
		
			  for(i=3; i<=one/2; i++)
		     {     
				/*check for prime number?*/
		      num1=1;
		      num2=1;
		      for(j=2; j<i; j++)
		       {     
		         if(i%j==0)
		           { num1=0;j=i;}
		        }
		      for(j=2; j<one-i; j++)
		       {     
		         if((one-i)%j==0)
		           { num2=0;j=one-i;}
		        }
		         if(num1==1 && num2==1)
		           { printf("%d =  %d + %d  \n",one,i,one-i); 
		           num3=1;}
		    }
         if(num3==0)
           {
				cout<<one<<"can not be expressed as sum of two prime numbers.\n\n";
			}
		}
		
		T4Tutorials (int two ,int three)
		{
			cout<<"Now checking Your input No.1 :"<<two;
			cout<<"\n\n";
			num1=1,num2=1,num3=0;
			  for(i=3; i<=two/2; i++)
		     {     
				/*---------- check for prime---------------*/
		      num1=1;
		      num2=1;
		      for(j=2; j<i; j++)
		       {     
		         if(i%j==0)
		           { num1=0;j=i;}
		        }
		      for(j=2; j<two-i; j++)
		       {     
		         if((two-i)%j==0)
		           { num2=0;j=two-i;}
		        }
		         if(num1==1 && num2==1)
		           { printf("%d =  %d + %d  \n",two,i,two-i); 
		           num3=1;}
		    }
         	if(num3==0)
            {
				cout<<two<<" can not be expressed as sum of two prime numbers.\n\n";
			}
			

			
			cout<<"\n\nNow checking Your input No.2 :"<<three;
			cout<<"\n\n";
			
			num1=1,num2=1,num3=0;
		
			  for(i=3; i<=three/2; i++)
		     {     
				/*---------- check for prime---------------*/
		      flg4=1;
		      flg5=1;
		      for(j=2; j<i; j++)
		       {     
		         if(i%j==0)
		           { flg4=0;j=i;}
		        }
		      for(j=2; j<three-i; j++)
		       {     
		         if((three-i)%j==0)
		           { flg5=0;j=three-i;}
		        }
		         if(flg4==1 && flg5==1)
		           { printf("%d =  %d + %d  \n",three,i,three-i); 
		           flg6=1;}
		    }
         if(flg6==0)
           {
				cout<<three<<"can not be expressed as sum of two prime numbers.\n\n";
			}
		}
			
};


int main()
{
	int option; 
	cout<<"Enter 1 of Single parameter T4Tutorialsor \n";
	cout<<"\nEnter 2 For Multiple Paramter constucor \n";
	cout<<"\nEnter 1 or 2 : ";
	cin>>option;
	system("cls");	//this function is to clear the screen
	if(option ==1)
	{
		cout<<"You Have Slected Single Paramater";
		cout<<"T4Tutorialsor\n";
		int one;
		cout<<"Enter number to check : ";
		cin>>one;
		T4Tutorials a(one);
	}
	else if(option==2)
	{
		cout<<"You Have slected Multiple Paramater ";
		cout<<"T4Tutorialsor\n";
		int two,three;
		cout<<"Enter First number to check : ";
		cin>>two;
		cout<<"\nEnter Second number to check : ";
		cin>>three;
		T4Tutorials a(two, three);
	}
	else
	{
		cout<<"Your Input in Wrong try Agin \n\n\n";
	}
}

 

 

Exit mobile version