Latest web development tutorials

C Exercise Example 84

100 cases of classic C language 100 cases of classic C language

Title: always an even number can be expressed as the sum of two prime numbers.

Program Analysis: I went to,what is this subject, I want to prove this problem? I really do not know how to prove. Then put an even number into two prime numbers it.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include<stdio.h>
#include<stdlib.h>
int Isprimer(unsigned int n);
int main()
{
    unsigned int n,i;
    do{
        printf("请输入一个偶数:\n");
        scanf("%d",&n);
    }while(n%2!=0);
    for(i=1;i<n;i++)
        if(Isprimer(i)&&Isprimer(n-i))
            break;
    printf("偶数%d可以分解成%d和%d两个素数的和\n",n,i,n-i);
    
    return 0;
}
int Isprimer(unsigned int n)
{
    int i;
    if(n<4)return 1;
    else if(n%2==0)return 0;
    else
        for(i=3;i<sqrt(n)+1;i++)
            if(n%i==0)return 0;
    
    return 1;
}

Run the above example output is:

请输入一个偶数:
4
偶数4可以分解成1和3两个素数的和

100 cases of classic C language 100 cases of classic C language