2025-03-06 22:46:33 +08:00

27 lines
452 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*-------------------------------------------------------
编写函数sum)计算1+2+3+…+n的和n由键盘输入。
-------------------------------------------------------*/
#include <stdio.h>
int sum(int n);
main()
{
int n,s;
printf("请输入一个整数n:");
scanf("%d",&n);
s=sum(n);
printf("1+2+...+%d=%d\n",n,s);
}
/**********Program**********/
int sum(int n){
int s = 0, i;
for(i=1;i<=n;i++){
s += i;
}
return s;
}
/********** End **********/