C语言程序设计100个经典例子
C语言作为一门广泛使用的高级编程语言,是计算机科学领域中最具影响力和应用广泛的语言之一。对于学习C语言的人来说,掌握一些经典的例子是非常重要的,它们可以帮助初学者理解C语言的基本概念和语法,培养编程思维和解决问题的能力。
下面将介绍100个经典的C语言程序设计例子,涵盖了C语言的各个方面,旨在帮助读者快速入门和提高编程水平。
1. Hello World程序一个完整的c语言程序
```c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
```
2. 输出整数
```c
#include <stdio.h>
int main() {
int num = 123;
printf("The number is: %d\n", num);
return 0;
}
```
3. 输入输出
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("The number you entered is: %d\n", num);
return 0;
}
```
4. 计算两个数的和
```c
#include <stdio.h>
int main() {
int num1 = 5, num2 = 3;
int sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}
```
5. 判断奇偶数
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
```
6. 计算阶乘
```c
#include <stdio.h>
int main() {
int num, i;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(i = 1; i <= num; ++i) {
factorial *= i;
}
printf("The factorial of %d is: %llu\n", num, factorial);
return 0;
}
```
7. 斐波那契数列
```c
#include <stdio.h>
int main() {
int n, i;
unsigned long long t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %llu, %llu, ", t1, t2);
for(i = 3; i <= n; ++i) {
nextTerm = t1 + t2;
printf("%llu, ", nextTerm);
t1 = t2;
t2 = nextTerm;
}
return 0;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论