#include
main()
{
int n,m=0,sum=0;
int i;
printf("input the number:\n");
scanf("%d",&n);
n*=2;
for(i=1;i<=n;i+=2)
{
sum+=i;
printf("sum=%d\n",sum);
m+=sum;
}
printf("m=%d\n",m);
}
#include "stdio.h"
void main()
{
int i,total,mid=0,sum1=0,sum2=0;
printf("Plsese enter the number:");
scanf("%d",&total);
for (i=1;i<=total;i++)
{
mid=i*2-1;
sum1=sum1+mid;
sum2=sum2+sum1;
}
printf("The result is:%d\n",sum2);
}
望采纳
注意到
1 + 3 + 5 + ... + 2n-1 = n*n
于是,直接写成
int sum = 0;
int i = 1;
int n = 7; // 这个数字你来定
for ( ; i<=n; i++ )
{
sum = sum + i*i;
}