c语言求1-2 3-4 5... 99-100
时间: 2023-04-27 10:06:27 浏览: 75
可以使用循环语句和条件语句来实现这个需求,具体代码如下:
```
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 100; i += 2) {
if (i == 99) {
printf("%d-%d\n", i, i + 1);
} else {
printf("%d-%d ", i, i + 1);
}
}
return 0;
}
```
运行结果为:
```
1-2 3-4 5-6 7-8 9-10 11-12 13-14 15-16 17-18 19-20 21-22 23-24 25-26 27-28 29-30 31-32 33-34 35-36 37-38 39-40 41-42 43-44 45-46 47-48 49-50 51-52 53-54 55-56 57-58 59-60 61-62 63-64 65-66 67-68 69-70 71-72 73-74 75-76 77-78 79-80 81-82 83-84 85-86 87-88 89-90 91-92 93-94 95-96 97-98 99-100
```
相关问题
用c语言实现1-2+3+4....+99-100
以下为示例代码:
```c
#include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
if (i % 10 == 1 && i % 100 != 11) {
printf("%d-st ", i);
} else if (i % 10 == 2 && i % 100 != 12) {
printf("%d-nd ", i);
} else if (i % 10 == 3 && i % 100 != 13) {
printf("%d-rd ", i);
} else {
printf("%d-th ", i);
}
}
return 0;
}
```
输出结果为:
```
1-st 2-nd 3-rd 4-th 5-th 6-th 7-th 8-th 9-th 10-th 11-th 12-th 13-th 14-th 15-th 16-th 17-th 18-th 19-th 20-th 21-st 22-nd 23-rd 24-th 25-th 26-th 27-th 28-th 29-th 30-th 31-st 32-nd 33-rd 34-th 35-th 36-th 37-th 38-th 39-th 40-th 41-st 42-nd 43-rd 44-th 45-th 46-th 47-th 48-th 49-th 50-th 51-st 52-nd 53-rd 54-th 55-th 56-th 57-th 58-th 59-th 60-th 61-st 62-nd 63-rd 64-th 65-th 66-th 67-th 68-th 69-th 70-th 71-st 72-nd 73-rd 74-th 75-th 76-th 77-th 78-th 79-th 80-th 81-st 82-nd 83-rd 84-th 85-th 86-th 87-th 88-th 89-th 90-th 91-st 92-nd 93-rd 94-th 95-th 96-th 97-th 98-th 99-th 100-th
```
c语言求1-2+3-4+…+99-100的值
要求解1-2 3-4 … 99-100的值,可以使用C语言编写一个程序来实现。首先,可以通过循环来遍历从1到99的所有奇数,然后利用每个奇数减去其后的偶数来得到结果。
下面是一个简单的C语言程序来实现这个任务:
```c
#include <stdio.h>
int main() {
int result = 0;
for (int i = 1; i <= 99; i += 2) {
result += i - (i + 1);
printf("%d-%d ", i, i+1);
}
printf("的值为:%d\n", result);
return 0;
}
```
在这个程序中,使用了一个for循环来遍历从1到99的所有奇数,然后每次迭代时将当前奇数与其后的偶数相减,并将结果累加到变量result上。最后,输出结果值。
当运行这个程序时,将会得到1-2 3-4 … 99-100的值为-50。这是因为1-2的结果是-1,3-4的结果是-1,以此类推,一直到99-100的结果是-1,所以将所有结果累加起来就得到了-50。