# 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.

## 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 = 0, n;        cout<< "Enter a positive integer: ";        //user input        cin>>n;        for(int i = 1; i <= 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(n-i))                        {                            cout<<n <<" = "<< i <<" + " << n-i<< endl;                            check = 1;                        }                }            }        if (check == 0)                cout<<n<<" cannot be expressed as the sum of two prime numbers.";        return 0;    }`
`OutputEnter a positive integer:1515=2+13 `
{"ft_lang_label":"__label__en","ft_lang_prob":0.73387533,"math_prob":0.99326414,"size":3635,"snap":"2021-43-2021-49","text_gpt3_token_len":1019,"char_repetition_ratio":0.25778022,"word_repetition_ratio":0.26804122,"special_character_ratio":0.32352132,"punctuation_ratio":0.083694085,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999044,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-08T00:49:50Z\",\"WARC-Record-ID\":\"<urn:uuid:6aca1ae3-108f-4df1-b0bf-1b6b4647b188>\",\"Content-Length\":\"133773\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:94917ca0-7922-498d-ab3f-20670fca03ce>\",\"WARC-Concurrent-To\":\"<urn:uuid:6c792e73-7e9d-41bb-9a9f-711822ee1bbf>\",\"WARC-IP-Address\":\"172.66.42.232\",\"WARC-Target-URI\":\"https://prepinsta.com/cpp-program/to-check-whether-a-number-be-expressed-as-a-sum-of-two-prime-numbers/\",\"WARC-Payload-Digest\":\"sha1:YEWSPEJI447HXJW3EDNPNDCJWJUP62NR\",\"WARC-Block-Digest\":\"sha1:52ENPTPDJZ3ZEXJNWWY3MA6ILZGCJU4T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363420.81_warc_CC-MAIN-20211207232140-20211208022140-00109.warc.gz\"}"}