zoukankan      html  css  js  c++  java
  • 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; 
    }
  • 相关阅读:
    linux部署zookeeper
    docker+fastdfs+springboot一键式搭建分布式文件服务器
    IDEA 设置springboot项目热部署
    定时备份docker部署的mysql数据
    离线安装docker
    PLSQL安装、PLSQL汉化、激活
    Mysql添加用户与授权
    MySql定时备份脚本
    mysql数据库定时备份
    实战申请Let's Encrypt永久免费SSL证书过程教程及常见问题
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14678170.html
Copyright © 2011-2022 走看看