Can A Number Be Expressed As A Sum Of Two Prime Numbers? | C Program

Number be expressed as a sum of two prime numbers in C

Here, we will discuss the program to check whether a number be expressed as a sum of two prime numbers in C. The program in C takes a positive integer or number which is required to be inserted by the user. The program further identifies whether that digit can be expressed as the sum of two prime numbers. If the inserted number can be expressed as sum of two prime numbers then, print the integer can be expressed as sum of two prime numbers as a result.

Number be expressed as a sum of two prime numbers in C

Theory

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.

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.

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

Algorithm

  • Take number as input in n
  • Initialize a variable flag as 0
  • Iterate using for loop from value of i between (2, n/2)
  •  For each iteration Call a function sum_of_two_primes for value of i is it returns 1
  • Call same function for value n-i and if it is also 1 then print i and n-i as answer increment the flag to 1
  • If flag is 0 print not possible
  • Create function sum_of_two_prime where check if passed number is prime return true else false
sum of two prime numbers

C code

Run
// C program to check whether a number can be expressed as a sum of two prime numbers

#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
    int n, i;

    printf("Insert the num: ");
    scanf("%d", &n);
    int flag = 0;
    for(i = 2; i <= n/2; ++i)
    {
        // Condition for i to be prime
        if(sum_of_two_primes(i) == 1)
        {
            if(sum_of_two_primes(n-i) == 1)
            {
                printf("%d can be expressed as the sum of %d and %d", n, i, n-i);
                flag = 1;
            }

        }
    }

    if(flag == 0)
        printf("%d cannot be expressed as the sum of two primes\n", n);
    return 0;
}

int sum_of_two_primes(int n)
{
    int i, isPrime = 1;
    for(i = 2; i <= n/2; ++i)
    {
       if(n % i == 0)
       {
          isPrime = 0;
          break;
       }
    }
    return isPrime;
}

Output

Insert the num: 15
15 can be expressed as the sum of 2 and 13

3 comments on “Can A Number Be Expressed As A Sum Of Two Prime Numbers? | C Program”


  • Raghu

    #include

    int isprime(int n) {
    if (n <= 1)
    return 0;

    for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
    return 0;

    return 1;
    }

    int main() {
    int num;
    scanf("%d", &num);

    int flag = 0;
    for (int j = 2; j <= num / 2; j++) {
    if (isprime(j) == 1 && isprime(num – j) == 1) {
    printf("%d %d\n", j, num – j);
    flag = 1;
    }
    }

    if (flag == 0)
    printf("No two prime numbers sum up to %d\n", num);

    return 0;
    }


  • sridharani

    #include
    //this method also prints all the possible prime numbers whose sum is given number
    int isitprime(int x)
    {
    int count=0;
    for(int i=2;i0)
    return 1;
    else return 0;
    }

    int main() {
    int n,flag=0;
    printf(“Enter the number”);
    scanf(“%d”,&n);
    for(int j=1;j<n/2;j++)
    {
    if(isitprime(j)==0 && isitprime(n-j)==0)
    printf("%d can be expressed as sum of %d and %d",n,j,n-j);
    flag=1;
    }
    if(flag==0)
    printf("%d cant be expressed as sum of two numbers.",n);
    return 0;
    }


  • Bhavana

    #include
    int prime(int j)
    { int k,count=0;
    for( k=1;k<=j;k++)
    {
    if(j%k==0)
    {
    count++;
    }
    }
    if(count==2)
    return 1;

    }
    int main()
    {
    int n,i;
    printf("enter the number:");
    scanf("%d",&n);
    for(i=2;i<=n/2;i++)
    {
    if(prime(i)==1)
    {
    if(prime(n-i)==1)
    {
    printf("the number %d can be written as sum of %d and %d",n,i,n-i);
    }
    }

    }
    }