zoukankan      html  css  js  c++  java
  • c语言中程序的循环控制,while语句

    c语言中程序的循环控制,while语句。

    1、输出从任一正整数到0的所有数字

    #include <stdio.h>
    
    int main(void)
    {
        int i;
        puts("please input an integer.");
        printf("i = "); scanf("%d", &i);
        
        while (i >= 0)
        {
            printf("%d ", i);
            i--;
        }
        putchar('\n');
        return 0;
    }

    2、c语言中程序的循环控制,while语句输出从0到任意正整数的所有数字。

    #include <stdio.h>
    
    int main(void)
    {
        int i = 0, j;
        puts("please input an integer.");
        printf("j = "); scanf("%d", &j);
        
        while (i <= j)
        {
            printf("%d ", i);
            i++;
        }
        putchar('\n');
        return 0;
    }

    3、c语言程序的循环控制,while语句限定次数的循环

    #include <stdio.h>
    
    int main(void)
    {
        int i;
        puts("please input an inter.");
        printf("i = "); scanf("%d", &i);
        
        while (i-- > 0)
        {
            putchar('*');
        }
        putchar('\n');
        return 0;
    }

    #include <stdio.h>
    
    int main(void)
    {
        int i = 0, j;
        puts("please input an integer.");
        printf("j = "); scanf("%d", &j);
        
        while (i < j)
        {
            putchar('*');
            i++;
        }
        putchar('\n');
        return 0;
    }

    4、c语言中程序的循环控制,while语句,输入指定个数数值,并计算他们的和及平均数

    #include <stdio.h>
    
    int main(void)
    {
        int i = 0, j, tmp, sum = 0;
        puts("please input an integer.");
        printf("j = "); scanf("%d", &j);
        
        while (i < j)
        {
            printf("tmp.%d = ", ++i); scanf("%d", &tmp);
            sum += tmp;
        }
        
        printf("sum = %d\n", sum);
        printf("mean = %.2f", (double)sum/j);
        return 0;
    }

    5、c语言中程序的循环控制,while语句将输入的正整数进行逆向输出

    #include <stdio.h>
    
    int main(void)
    {
        int i;
        do
        {
            puts("please input an integer.");
            printf("i = "); scanf("%d", &i);
            if (i <= 0)
                puts("the range of i is > 0.");
        }
        while (i <= 0);
        
        while (i > 0)
        {
            printf("%d", i % 10);
            i /= 10;
        }
        putchar('\n');
        return 0;
    }
  • 相关阅读:
    Asp.Net Core&Docker部署到树莓派3B中
    KnockoutJS-与服务端交互
    服务部署到Swarm Cluster中
    新建项目加入到生成流水线中
    约定Service构建方式
    约定Jenkins构建脚本
    约定新项目的搭建流程
    设计生成自动化流水线
    新建项目到Jenkins中
    把ABP框架部署到Docker中
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14628914.html
Copyright © 2011-2022 走看看