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;
    }
  • 相关阅读:
    linux----别名
    linux---三剑客
    如何給linux安装 jdk呢?
    linux---文件颜色含义
    reason: no instance(s) of type variable(s) exist so that ProjectByProvinceVO conforms to ProjectDetailVO
    jrebel的坑
    使用自定义注解,但是运行时获取不到注解的值
    springboot启动失败,没有任何提示,trycatch也没有输出
    case when 中计数如何去重
    case when 中如何使用计数
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14628914.html
Copyright © 2011-2022 走看看