1. 程式人生 > >C語言學習之數列求和

C語言學習之數列求和

#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();
}

結果截圖