c语言 对任意偶数n(n

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/27 13:26:46
c语言对任意偶数n(nc语言对任意偶数n(nc语言对任意偶数n(n#include<stdio.h>#include<math.h>int main(void){&n

c语言 对任意偶数n(n
c语言 对任意偶数n(n

c语言 对任意偶数n(n
#include<stdio.h>
#include<math.h>
int main(void)

    int x,i,j,f;  
    
    do   
    { 
         printf("请输入偶数:");     
         scanf("%d",&x);   
    } while(x%2||x<3); //这两个条件是或者的关系,你改成&&肯定有问题的,这个条件限制输入数据是偶数并且要大于3才可以
                       //当x为技术时满足条件1,当x小于3时,满足条件2,都会导致重复do..while循环 
                   //这个限制天剑没看懂.而且我把他改为(x%2!=0&&x<3)时出现了看不懂的错...  
    
    printf("偶数:%d\n素数对:\n",x);  
    
    for(i=2;i<=x/2;i++)              //遍历2~x/2之间的数  
    {                                
                                     //判断数i是否为素数 
                                     f=1;     
                                     for(j=2;j<=sqrt(i);j++)      
                                     if(i%j==0) f=0; 
                                     
                                     //f=1表示i为素数   
                                     if(f)      
                                     { 
                                                //判断x-i是否为素数 
                                                for(j=2;j<=sqrt(x-i);j++)         
                                                if((x-i)%j==0) f=0;        
                                                
                                                //如果是,输出结果 
                                                if(f)         
                                                printf("%d,%d\n",i,x-i);       
                                     }    
    } 
}