用C语言编写程序:输入一个三位数,输出每位数的平方和(用for循环结构)

来源:百度知道 编辑:UC知道 时间:2024/06/18 12:46:15

#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