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