Coders Packet

C++ code to check if a number is sum of two prime number


This is a C++ code to find whether a given positive number is a sum of any two prime numbers or not. If it is the sum of other prime numbers, then it shows all the combinations of the latter.

This given C++ is a code that finds all the combinations of prime numbers that sum up to the given positive number. It uses the fact that 0 and 1 are not prime numbers hence we check all the numbers from 2 to n/2 for if that number is prime or not. If we find that a number is prime then we check n-i for prime and if we get both (i) and (n-i) as a prime then we print them. To check for prime we simply use the basic % operator method.

Note:- In the 37th line we can also use (i<=sqrt(n)) instead of (i<=n/2) to decrease time complexity.

Download project

Reviews Report

Submitted by AADITYA RAJ (AadiBaby)

Download packets of source code on Coders Packet