其他分享
首页 > 其他分享> > 【明解C语言】第四章--代码清单和练习

【明解C语言】第四章--代码清单和练习

作者:互联网

 

代码清单4-1--判断奇偶数

int main()
{
    int cont;
    do 
    {
        int no;
        printf("请输入一个整数:");
        scanf("%d", &no);

        if (no % 2)
        {
            printf("这是奇数");
        }
        else {
            printf("这是偶数");
        }
        printf("要重复一次吗");
        scanf("%d", &cont);


    } while (cont == 0);
    return 0;
}

代码清单4-3
int main()
{
    int num;
    int dig;
    do {
        printf("请输入一个非负整数:");
        scanf("%d", &num);
        if (num < 0)
        {
            puts("\a请不要输入负数");
        }
    } while (num < 0);

    dig = 0;
    do {
        num /= 10;//每次退掉末尾数
        dig += 1;//每次进入do,计数加1

    } while (num > 0);
    printf("该整数的位数%d\n", dig);
    return 0;
}

 

//代码清单4-4--求和
int main()
{
    int no = 1;
    int sum = 0;

    do {
        sum += no;//1 1+2  1+2+3 1+2+3+4 1+2+3+4+5
        no += 1;    //2 3 4  5 6跳出循环
    } while (no <= 5);
    printf("1到5的合计值是%d\n", sum);
    return 0;
}

 

//练习4-1--逆向输出+计数
int main()
{
    int no = 0;
    int i = 0;
    printf("请输入一个非负整数:");
    scanf("%d", &no);
    do
    {
        

        printf("%d\n", no % 10);
        no /= 10;
        i+=1;

    } while (no>0);
    printf("该数的位数是i=%d\n", i);
    return 0;
}

 

//练习4-3
int main()
{
    int num1, num2, sum;
    puts("请输入两整数");
    scanf("%d %d", &num1, &num2);

    int i;
    for (i = num1,sum=0; i <= num2; i++)
    {
        sum += i;
    }
    printf("%d到%d之间的所有整数和是%d", num1,num2,sum);
    return 0;
}

int main()
{
    int n1, n2;
    int sum = 0;
    puts("请输入两个数:");
    scanf("%d %d", &n1, &n2);

    int i = n1;
    do
    {

        sum += i;
        i++;
    } while (i <= n2);
    printf("%d到%d的合计值是%d", n1, n2, sum);
    return 0;
}

 

 

//代码清单4-5
int main()
{
    int no = 1;
    int sum = 0;

    do
    {
        sum += no;
        no += 1;
    } while (no <= 5);

    printf("1到5的合计值是%d", sum);
    return 0;
}

 

//代码清单4-6--n+=1和n++
int main()
{
    int no = 1;
    int sum = 0;

    do
    {
        sum += no;
        no++;
    } while (no <= 5);

    printf("1dao5的合计值是%d", sum);
    return 0;
}

 

//练习4-4--使用/= 替换n=n/10
//练习4-5 使用/=和++替换n=n/10,n+=1
int main()
{
    int num;

    do
    {
        printf("请输入一个非负整数:");
        scanf("%d", &num);
        if (num < 0)
            puts("请不要输入负数");

    } while (num < 0);

    printf("该整数的逆向显示结果是:");
    do
    {
        printf("%d", num % 10);//取出末尾数字
        num /= 10;//重新赋值
    } while (num > 0);
    puts("。");
    return 0;
}


int main()
{
    int num;
    int dig;
    do
    {
        printf("请输入一个非负整数:");
        scanf("%d", &num);
        if (num < 0)
            puts("\a请不要输入负数。");
    } while (num < 0);

    dig = 0;
    do
    {
        num /= 10;//每次舍弃末尾数字
        dig += 1;//计数器加1

    } while (num > 0);
    printf("该整数的位数是%d",dig);

    return 0;
}

 

 

--------------------------------------------------------

while循环

// while循环
//代码清单4-7
int main()
{
    int no;
    printf("请输入一个整数:");
    scanf("%d", &no);

    while (no >= 0)
    {
        printf("%d", no);
        no--;
    }
    putchar("\n");//回显字符串
    return 0;
}

 

//练习4-6
int main()
{
    int no;
    printf("请输入一个正数:");
    scanf("%d", &no);

    while (no >= 0)
    {
        printf("%d", no--);

    }
    putchar("\n");
    return 0;
}

 

 

//代码清单4-9
int main() 
{
   int i, no; printf("请输入一个正数:"); scanf("%d", &no); i = 0; while (i <= no) { printf("%d", i++); } putchar('\n'); return 0; }

 

//代码清单4-11
int main()
{
    int i = 0;
    int sum = 0;
    int num,tmp;

    printf("要输入多少个整数:");
    scanf("%d", &num);//3

    while (i < num)//i<3 0 1 2 3-退出
    {
        printf("NO.%d",++i); //1 2 3
        scanf("%d", &tmp);    //4 5 6
        sum += tmp;                    //4 9 15
    }

    printf("合计值:%d\n", sum);//15
    printf("平均值:%.2f\n", (double)sum / num);//5
    return 0;
}

 

---------------------------------------------------

4-3for语句

//代码清单4-12--简单的遍历
int main()
{
    int i, no;
    printf("请输入一个正数:");
    scanf("%d", &no);

    for (i = 0; i <= no; i++)
    {
        printf("%d", i);
    }
    putchar('\n');
    return 0;
}

 

 

//代码清单4-13--for循环输出字符串
int main()
{
    int i, no;
    printf("请输入一个正数:");
    scanf("%d",&no);

    for (i = 1; i <= no; i++)
        putchar('*');
    putchar('\n');
    return 0;
}

 

 

//代码清单4-14
int main()
{
    int i;
    int sum = 0;
    int num, tmp;

    printf("输入多少个整数:");
    scanf("%d", &num);//3

    for (i = 0; i < num; i++)//0 1 2 3-退出
    {
        printf("No.%d", i + 1);//1 2 3
        scanf("%d", &tmp);    //1 3 5
        sum += tmp;                    //1 4 9
    }
    printf("合计值:%d\n", sum);//9
    printf("平均值:%.2f\n", (double)sum / num);//3.00
    return 0;
}

 

 

//练习4-12--对代码清单4-6修改,计算1~5的和
int main()
{
    int no = 0;
    int sum = 0;

    for (no = 1; no <= 5; no++)
        sum += no;
    printf("1~5的合计值是:%d",sum);
    return 0;
}

 

标签:no,--,明解,C语言,int,while,num,printf,main
来源: https://www.cnblogs.com/sdr900/p/16483118.html