采用c语言编程实现以下功能:用3种循环结构编写程序实现输出1到10的平方之和

如题所述

#include<stdio.h>
void main()
{
int a,b,c,n,m;
scanf("%d",&n);//输入数据
a=n%10%10;//提取个位数
b=n%100/10;//提取十位数
c=n/100;//提取百位数
m=a*a+b*b+c*c;
printf("%d\n",m);
}
输入123
输出14
改成这样就不受位数的限制了:
#include<stdio.h>
void main()
{
int a,n,m;
scanf("%d",&n);
m=0;
while(n!=0)
{
a=n%10;
n/=10;
m+=a*a;
}
printf("%d\n",m);
}
输入123
输出14
输入1231
输出15
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-04
代码一:
#include <stdio.h>
int main()
{int i,s=0;
 for(i=1;i<11;i++)
   s+=i*i;
 printf("%d\n",s);
 return 0;
}

代码二:
#include <stdio.h>
int main()
{int i=1,s=0;
 while(i<11)
 {s+=i*i;
  i++;
 }
 printf("%d\n",s);
 return 0;
}

代码三:
#include <stdio.h>
int main()
{int i=1,s=0;
 do
 {s+=i*i;
  i++;
 }while(i<11);
 printf("%d\n",s);
 return 0;
}

本回答被网友采纳
第2个回答  2011-09-08
for(int i = 1;i <= 10;i ++)
{
printf("%d",i*i);
}你还可以适用 while()
{

}
do
while()
{}
相似回答