Virtual base class C++ Program to 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 by using the virtual base class in Object Oriented Programming.
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 | #include<iostream> using namespace std; class T4Tutorials_Grand_Class { public : int n; }; class p1 : public virtual T4Tutorials_Grand_Class { public : int T4Tutorials_Var3,j; }; class p2:public virtual T4Tutorials_Grand_Class { public : int i; }; class derived : public p1,public p2 { protected : int T4Tutorials_Var1,T4Tutorials_Var2,T4Tutorials_Var3,j; public : int checK() { T4Tutorials_Var1=1,T4Tutorials_Var2=1,T4Tutorials_Var3=0; cout<<"Program will check that the Number can be Express as Sum of Two Prime Numbers:"; cout<<"Input a positive integer: "; cin>>n; for(i=3; i<=n/2; i++) { T4Tutorials_Var1=1; T4Tutorials_Var2=1; for(j=2; j<i; j++) { if(i%j==0) { T4Tutorials_Var1=0;j=i;} } for(j=2; j<n-i; j++) { if((n-i)%j==0) { T4Tutorials_Var2=0;j=n-i;} } if(T4Tutorials_Var1==1 && T4Tutorials_Var2==1) { cout<<n, i, n-i; T4Tutorials_Var3=1;} } if(T4Tutorials_Var3==0) { cout<<"can not be expressed as sum of two prime numbers."<<n<<endl; } } }; int main() { derived a; a.checK(); } |
Output
Program will check that the Number can be Express as Sum of Two Prime Numbers:
Input a positive integer:9
9 can not be expressed as sum of two prime numbers.
FAQ
Develope a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers by using the virtual base class and for loop and if else statement.