c语言1+11+111+1111+11111
时间: 2024-01-26 19:01:46 浏览: 319
以下是C语言求解1+11+111+1111+11111的例子:
```c
#include <stdio.h>
int main() {
int sum = 0;
int num = 1;
int count = 0;
for (int i = 1; i <= 5; i++) {
sum += num;
num = num * 10 + 1;
}
printf("Sum: %d\n", sum);
while (sum > 0) {
if (sum % 10 == 1) {
count++;
}
sum /= 10;
}
printf("Number of 1s: %d\n", count);
return 0;
}
```
输出结果为:
```
Sum: 12345
Number of 1s: 5
```
阅读全文