2025-03-06 23:47:59 +08:00

33 lines
582 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,其功能是用While循环语句求1到n之间包括n)能
被3整除的所有整数之和,并将结果返回给主函数。n值由主函数传入
运行程序后若输入10则输出为18
----------------------------------------------------------------------*/
#include "stdio.h"
long sum(int n)
{
/**********Program**********/
int i, s=0;
for(i=1;i<=n;i++){
if(i%3==0){
s+=i;
}
}
return s;
/********** End **********/
}
void main()
{
int x;
long f;
scanf("%d",&x);
f=sum(x);
printf("%ld\n",f);
}