#include
#include
int main()
{
int n = 1;
int sum=0;
do
{
sum += (int)pow((double)n++, 2);
} while ( sum < 10000 );
printf( "n=%d, sum=%d\n", n, sum );
}
用递归算法,代码如下:
#include "stdio.h"
void main()
{
int a=1,b=0,c;
while(b<10000)
{
c=a*a;
b=b+c;
a+=1;
}
printf("1^2+2^2+3^2+......+%d^2=%d",a,b);
}
#include
void main ()
{
int sum=0,n;
for(n=1;sum<10000;n++)
{
sum+=n*n;
}
count<<"n="<
#include
int main(int argc, char *argv[])
{int sum=0,n=1,s;
while (sum<=10000)
{
s=n*n;
sum+=s;
n++;
}
printf("n=%d,sum=%d\n",n,sum);
return 0;
}
#include
void main()
{
int i=0,sum=0;
while(sum<1000)
{
sum+=i*i;
i++;
}
printf("n=%d sum=%d\n",i-1,sum);
}