Wednesday, September 12, 2018

PRIME FACTORS

  • Problem Description

    Helan had to find a number that must be the sum of two prime numbers. Help him to write a C Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
  • CODING ARENA::
  • #include <stdio.h>
    int checkPrime(int n);
    int main()
    {
        int n, i, flag = 0;

        
        scanf("%d", &n);

        for(i = 2; i <= n/2; ++i)
        {
            
            if (checkPrime(i) == 1)
            {
               
                if (checkPrime(n-i) == 1)
                {
                   
                    printf("%d = %d + %d\n", n, i, n - i);
                    flag = 1;
                }

            }
        }

        if (flag == 0)
            printf("NOT");

        return 0;
    }


    int checkPrime(int n)
    {
        int i, isPrime = 1;

        for(i = 2; i <= n/2; ++i)
        {
            if(n % i == 0)
            {
                isPrime = 0;
                break;
            }  
        }

        return isPrime;
    }
  • Test Case 1

    Input (stdin)
    35
    
    
    Expected Output
    NOT
  • Test Case 2

    Input (stdin)
    34
    
    
    Expected Output
    34 = 3 + 31
    
    34 = 5 + 29
    
    34 = 11 + 23
    
    34 = 17 + 17

1 comment: