c语言小程序代码大全
时间: 2023-08-22 09:13:19 浏览: 39
以下是一些常见的C语言小程序示例代码:
1. Hello World
```c
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
```
2. 求和
```c
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum of %d and %d is %d", a, b, sum);
return 0;
}
```
3. 判断奇偶数
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even", num);
} else {
printf("%d is odd", num);
}
return 0;
}
```
4. 判断闰年
```c
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d is a leap year", year);
} else {
printf("%d is not a leap year", year);
}
return 0;
}
```
5. 计算阶乘
```c
#include <stdio.h>
int main() {
int num, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is %d", num, fact);
return 0;
}
```
这些只是一些简单的示例,C语言可以用于开发更复杂的应用程序,如果您想了解更多,请参考相关的教程和文档。