Write a program in C++ to Check Whether a Number can be Express as Sum of Two Prime Numbers.

#include <iostream>
using namespace std;
void main()
{

   int n, i, flg1=1,flg2=1,flg3=0,j;
       cout<<"Check Whether a Number can be Express as Sum of Two Prime Numbers:\n ";
    cout<<"Input  a positive integer: ";
    cin>>n;
   for(i=3; i<=n/2; i++)
     {    
    flg1=1;
      flg2=1;
      for(j=2; j<i; j++)
       {    
         if(i%j==0)
           { flg1=0;j=i;}
        }
      for(j=2; j<n-i; j++)
       {    
         if((n-i)%j==0)
           {
  flg2=0;j=n-i;
  }
       }
         if(flg1==1 && flg2==1)
           {
  cout<<n<<" = "<< i<<" +"<< n-i <<endl;
           flg3=1;
  }
       }
         if(flg3==0)
           {
  cout<<n<<" can not be expressed as sum of two prime numbers.\n";
}
      cout<<endl;
         system ("pause");
}


Test Data :
Input a positive integer: 16
Expected Output :
16 = 3 + 13
16 = 5 + 11
.

Write a program in C++ 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. Reviewed by Unknown on June 20, 2017 Rating: 5

No comments:

Powered by Blogger.