C语言——循环结构 任意输入一个整数m,若m不是素数,则从小到大输出其所有不包括1和自身的因子;否则输出“没有因子,

任意输入一个整数m,若m不是素数,则从小到大输出其所有不包括1和自身的因子;否则输出“没有因子,是素数”的相关提示信息。
输入提示信息:"Please enter a number:"
输入格式:"%d"
输出格式:
有因子时:"%d\n"
无因子时:"It is a prime number.No divisor!\n"
输入的整数小于2时输出:"It is not a prime number.No divisor!\n"
对应的英文题目:
Input an integer m arbitrarily. If M is not a prime number, output all its factors excluding 1 and itself from small to large; Otherwise, the relevant prompt information of "no factor, prime number" will be output.
Enter a prompt:
Input format:
Output format:
When there is a factor: "% d \ n"
"It is a prime number. No director! \ n"
Output when the input integer is less than 2: "it is not a prime number. No director! \ n"

代码如下:

#include  <math.h>
#include  <stdio.h>
main()
{        	 
    int  m, i;
    int  flag = 1;               //置标志变量flag初值为真(1)
    printf("Please enter a number:");
    scanf("%d", &m);
    if (m <= 1)                  //负数,0和1都不是素数
    {        	 
        flag = 0;                //将标志变量flag置为假(0)
        if (1 == m || -1 == m || 0 == m)
        {        	 
            printf("It is not a prime number.No divisor!\n");
        }
        m = (int)fabs(m);       //对m取绝对值,以便对负数输出因子但不输出正负号
    }
    for (i = 2; i <= m - 1; i++)
    {        	 
        if (m % i == 0)          //若m能被i整除,表明i是m的一个因子
        {        	 
            flag = 0;             //则将标志变量flag置为假(0)
            printf("%d\n", i);  //输出m的因子i
        }
    }
    if (flag)   //若标志变量flag为真,则表明全部检验完毕,未发现能整除m的数
    {        	 
        printf("It is a prime number.No divisor!\n");//m是素数,没有因子
    }
}        	 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值