c语言代码实例

时间:2025-02-14 10:13:47 单机游戏

Hello, World!

```c

include

int main() {

printf("Hello, World!\n");

return 0;

}

```

计算两个整数的和

```c

include

int main() {

int num1, num2, sum;

num1 = 5;

num2 = 3;

sum = num1 + num2;

printf("两数之和为: %d\n", sum);

return 0;

}

```

判断一个数是否为偶数

```c

include

int main() {

int num;

num = 8;

if (num % 2 == 0) {

printf("%d是偶数\n", num);

} else {

printf("%d是奇数\n", num);

}

return 0;

}

```

猜数字游戏

```c

include

include

include

int main() {

int num, guess;

srand(time(0));

num = rand() % 100 + 1;

do {

printf("请输入一个数字(1-100):");

scanf("%d", &guess);

if (guess > num) {

printf("太大了!\n");

} else if (guess < num) {

printf("太小了!\n");

}

} while (guess != num);

printf("猜对了!\n");

return 0;

}

```

输出九九乘法表的左上半角

```c

include

int main() {

int k, i, j;

for (i = 1; i <= 9; i++) {

for (j = 1; j <= i; j++) {

printf("%d*%d=%d\t", j, i, i * j);

}

printf("\n");

}

return 0;

}

```

这些实例涵盖了C语言的基本输入输出、条件判断、循环等基本概念。希望这些示例对你有所帮助。