-
你需要初始化
sum
和
length
到
0
否则,它们会保留垃圾价值
-
接受输入的循环不正确。你需要
do {
...} while (x <= 0);
-
您需要交换这两条线,使其成为:
int digit = (int) (x % 10);
x = x/10;
第
迭代,数字将获得i+1
第
第
数字
-
else (...)
无效语法。你需要
else if (...)
.
#include <stdio.h>
int main(void)
{
long long cc_number;
long long x;
long long length = 0;
int sum = 0, count = 0;
cc_number = (x = 54560);
while (x != 0)
{
x= x/10;
length++;
}
x= cc_number;
while (x != 0)
{
x= x/10;
int digit= (int) (x % 10);
count++;
if ((count % 2 == 0) && (length %2 ==1))
{
sum=sum+digit;
}
else if((count % 2 == 1) && (length %2 ==0))
{
sum=sum+digit;
}
}
printf("The sum is %i\n", sum);
printf("The length of the digits is %lli\n", length);
}
这是打印出来的
$ ./a.out
The sum is 10
The length of the digits is 5