首页 > 编程语言 > 详细

c语言4-19 在显示所输入的数值的所有约数之后,显示约数的个数

时间:2021-04-20 23:25:58      阅读:38      评论:0      收藏:0      [点我收藏+]

 

1、原始程序

#include <stdio.h>

int main(void)
{
    int i = 1, j;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 0)
            puts("the range of j is : > 0 ");
    }
    while (j <= 0);
    
    while (i <= j)
    {
        if (j % i == 0)
            printf("%d\n", i);
        i++;
    }
    return 0;
}

 

2、while语句

#include <stdio.h>

int main(void)
{
    int i = 1, j, cnt = 0;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 0)
            puts("the range of j is : > 0 "); 
    }
    while (j <= 0);
    
    while (i <= j)
    {
        if (j % i == 0)
        {
            printf("%d\n", i);
            cnt++; 
        }
        i++;
    }
    printf("the number of devisor is %d\n", cnt);
    
    return 0;
}

 

3、for语句

#include <stdio.h>

int main(void)
{
    int i, j, cnt = 0;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 0)
            puts("the range of j is > 0 ");
    }
    while (j <= 0);
    
    for (i = 1; i <= j; i++)
    {
        if (j % i == 0)
        {
            printf("%d\n", i);
            cnt++;
        }
    }
    printf("the number of devisor is %d\n", cnt);
    
    return 0;
}

 

4、do语句

#include <stdio.h>

int main(void)
{
    int i = 1, j, cnt = 0;
    puts("please input an integer.");
    do
    {
        printf("j = "); scanf("%d", &j);
        if (j <= 0)
            puts("the range of j is > 0"); 
    }
    while (j <= 0);
    
    do
    {
        if (j % i == 0)
        {
            printf("%d\n", i);
            cnt++;
        }
        i++;
    }
    while (i <= j);
    printf("the number of divesor is %d\n", cnt);
    
    return 0;
}

 

c语言4-19 在显示所输入的数值的所有约数之后,显示约数的个数

原文:https://www.cnblogs.com/liujiaxin2018/p/14682998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!