#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
温馨提示:答案为网友推荐,仅供参考