In C language, you can use loops to calculate sum. Here is an example code:

#include <stdio.h>

int main() {
    int n, sum = 0;
    
    printf("请输入一个整数 n:");
    scanf("%d", &n);
    
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    
    printf("1 到 %d 的和为:%d\n", n, sum);
    
    return 0;
}

The program begins by taking an integer n input from the user, then it iteratively sums up from 1 to n, and finally outputs the result of the addition.

广告
Closing in 10 seconds
bannerAds