C++ Program to check whether a number be expressed as a sum of two prime numbers

Program to check whether a number be expressed as a sum of two prime numbers

Prime number is a number which only have two divisors i.e. a number which can not be divided by any other number other than 1 or itself is a prime number.

There are many theories which express numbers as a sum of two primes like Goldbach’s Conjecture which states that any even number greater than 2 can be expressed as a sum of two primes.

Here we will check for all the numbers if they can be expressed as sum of two primes or not.

Program to express numbers as a sum of two primes

Algorithm:-

  1. Take number as input from user.
  2. Input is stored in an int type variable say num 
  3. A loop is started from i = 1 and will work till  i <= num/2
    1. Every value of i is checked for being a prime number.
    2. If i is a prime number then num – i is checked for being a prime.
    3. If both are prime then print the result
    4. Otherwise num cannot be expressed as a sum of two prime numbers.

C++ Code:-

    
    //C++ Program
    //Number expressed as sum of two prime numbers
    #include<iostream>
    using namespace std;
    // Function to check prime number
    int Prime(int num)
    {
        int div=0;
        for(int i=1;i<=num;i++)
        {
            if(num%i==0)
                div++;                
        }
        if(div==2)
            return 1;
        return 0;
    }
    int main()
    {
        int check = 0n;
        cout<< “Enter a positive integer: “;
        //user input
        cin>>n;
        for(int i = 1i <= n/2;i++)
        {
                // condition for i to be a prime number
                if (Prime(i))
                {
                        // condition for n-i to be a prime number
                        if (Prime(ni))
                        {
                            cout<<n <<” = “<< i <<” + “ << ni<< endl;
                            check = 1;
                        }
                }    
        }
        if (check == 0)
                cout<<n<<” cannot be expressed as the sum of two prime numbers.”;
        return 0;
    }

    Output

    Enter a positive integer: 15
    15 = 2 + 13
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python