c语言编程1到100偶数怎么编

时间:2025-01-28 22:47:20 网络游戏

方法一:使用数组模拟手工乘法

```c

include

define MAXN 200

int main() {

int a[MAXN];

int len = 1;

a = 2;

for (int i = 4; i <= 100; i += 2) {

int carry = 0;

for (int j = 0; j < len; j++) {

int temp = a[j] * i + carry;

a[j] = temp % 10;

carry = temp / 10;

}

while (carry) {

a[len++] = carry % 10;

carry /= 10;

}

}

for (int i = len - 1; i >= 0; i--) {

printf("%d", a[i]);

if (i != 0) {

printf(" ");

}

}

printf("\n");

return 0;

}

```

方法二:使用for循环和if语句

```c

include

int main() {

for (int i = 2; i <= 100; i += 2) {

printf("%d ", i);

}

printf("\n");

return 0;

}

```

方法三:使用while循环

```c

include

int main() {

int i = 2;

while (i <= 100) {

printf("%d ", i);

i += 2;

}

printf("\n");

return 0;

}

```

方法四:使用递归函数

```c

include

void printEvenNumbers(int n) {

if (n <= 0) return;

printf("%d ", n);

printEvenNumbers(n - 2);

}

int main() {

int n = 100;

printEvenNumbers(n);

printf("\n");

return 0;

}

```

方法五:使用数学公式计算偶数和

```c

include

int main() {

int sum = (2 + 100) * 50 / 2;

printf("%d\n", sum);

return 0;

}

```

以上方法都可以实现输出1到100之间的所有偶数。你可以根据自己的需求和编程习惯选择合适的方法。