C语言编程:输入一个整数,求它的位数以及各位数字之和。

2024-11-25 09:22:27
推荐回答(3个)
回答1:

#include

int main()
{
int n,count,sum,tmp;
printf("Input an integer:");
while(scanf("%d",&n)!=EOF && n>=0)
{
count=1;
sum=0;
while(n>0)
{
tmp=n%10;
sum+=tmp;
n = n/10;
if(n>0)
{
count++;
}
}
printf("count=%d,sum=%d\n",count,sum);
printf("Input an integer:");
}
return 0;
}

回答2:

#include
int main(){ printf("input an integer:");
int n = 0;
scanf("%d",&n);
int sum = 0;
int i = 1;
//求出数的位数
for (;;i++)
{
n = n / 10;
if(n == 0)
break;
}
n = nBak;
for(;;)
{
int tempa = 1;
for(int j = 0;j < i; j++ )
{
tempa *= n % 10;
}
sum += tempa;
n = n / 10;
if(n == 0)
break;
} printf("count:=%d, sum= %d",i,sum);return 0; }

回答3:

void main()
{
int a,tmp,sum=0,i=0;
printf("Input an integer:");
scanf("%d",&a);
while(a>=10)
{
sum+=a%10;
a=a/10;
i++;
}
i++;
sum+=a;
printf("count=%d,sum=%d\n",i,sum);
}