检查一个数能不能表示成两个质数之和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caozl218/article/details/73302625
#include <stdio.h>
int prime(int n);
int main()
{
    int n, i, flag=0;
    printf("Enter a positive integer: ");
    scanf("%d",&n);
    for(i=2; i<=n/2; ++i)
    {
        if (prime(i)!=0)
        {
            if ( prime(n-i)!=0)
            {
                printf("%d = %d + %d\n", n, i, n-i);
                flag=1;
            }
 
        }
    }
    if (flag==0)
      printf("%d can't be expressed as sum of two prime numbers.",n);
    return 0;
}
int prime(int n)      /* Function to check prime number */
{
    int i, flag=1;
    for(i=2; i<=n/2; ++i)
       if(n%i==0)
          flag=0;
    return flag;
}

结果输出:

Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页