现在的位置: 首页 > 综合 > 正文

C语言学习之数列求和

2013年01月02日 ⁄ 综合 ⁄ 共 395字 ⁄ 字号 评论关闭
文章目录

计算1+1+2+1+2+3+1+2+3+4+.....+1+2+3+4+……+n的值。

代码

#include <stdio.h>
#include "conio.h"

int main()
{

    int sum2 = 0;
    int n = 0;
    printf("please enter the largest number of this caculate: \n");
    scanf("%d",&n);

    if(n < 0)
    {
        printf("the entered number shouldn't be a negetive number!!");
        getch();
        return 1;
    }
    else
    {
        for(int i = 1; i <= n; i++)
        {
            int sum1 = 0;
            for(int j = 1; j <= i; j++)
            {
                sum1 +=j;
            }
            sum2 += sum1;
        }
    }

    printf("the result of caculate(%d) is %d\n",n,sum2);
    getch();
}

结果截图

抱歉!评论已关闭.