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.
C++ Program with constructor to Check Whether a Number can be Express as Sum of Two Prime Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #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"; } } |