• c语言413 求1到n的和


    1、while语句

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

    2、while语句;

    #include <stdio.h>
    
    int main(void)
    {
        int i = 1, j, sum = 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);
        
        printf("the sum of 1 to %d is: ", j);
        while (i <= j)
        {
            sum += i;
            i++;
        }
        printf("%d\n", sum);
        
        return 0;
    }

    3、for语句

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

    4、for语句

    #include <stdio.h>
    
    int main(void)
    {
        int i, j, sum = 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);
        
        printf("the sum of 1 to %d is: ", j);
        for (i = 1; i <= j; i++)
        {
            sum += i;
        }
        printf("%d\n", sum);
        
        return 0;
    }

    5、do语句

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

    6、do语句

    #include <stdio.h>
    
    int main(void)
    {
        int i = 1, j, sum = 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);
        
        printf("the sum of 1 to %d is: ", j);
        do
        {
            sum += i;
            i++;
        }
        while (i <= j);
        printf("%d\n", sum);
        
        return 0; 
    }
  • 相关阅读:
    PostgreSQL杂记页
    程序员给女朋友周年纪念的浪漫
    实现乐鑫esp8266的无线OTA升级,实现远程在线升级固件
    XAOP的使用示范例子
    XPage的使用示范例子
    Python手势识别与控制
    Python人体肤色检测
    基于Opencv自带BP网络的车标简易识别
    iOS 自定义转场动画
    Python实时语音识别控制
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14678170.html
Copyright © 2020-2023  润新知