admin 管理员组

文章数量: 887021


2023年12月22日发(作者:电脑安装包怎么安装)

C语言程序设计100个经典例子

C语言作为一门广泛使用的高级编程语言,是计算机科学领域中最具影响力和应用广泛的语言之一。对于学习C语言的人来说,掌握一些经典的例子是非常重要的,它们可以帮助初学者理解C语言的基本概念和语法,培养编程思维和解决问题的能力。

下面将介绍100个经典的C语言程序设计例子,涵盖了个方面,旨在帮助读者快速入门和提高编程水平。1. Hello World程序

```c

#include

int main() {

printf("Hello, World!n");

return 0;

}

```

2. 输出整数

```c

#include

int main() {

C语言的各

int num = 123;

printf("The number is: %dn", num);

return 0;

}

```

3. 输入输出

```c

#include

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("The number you entered is: %dn", num);

return 0;

}

```

4. 计算两个数的和

```c

#include

int main() {

int num1 = 5, num2 = 3;

int sum = num1 + num2;

printf("The sum of %d and %d is: %dn", num1, num2, sum);

return 0;

}

```

5. 判断奇偶数

```c

#include

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

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: %llun", num, factorial);

return 0;

}

```

7. 斐波那契数列

```c

#include

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;

}

```

8. 判断素数

```c

#include

int main() {

int num, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &num);

for(i = 2; i <= num/2; ++i) {

if(num % i == 0) {

flag = 1;

break;

}

}

if (flag == 0) {

printf("%d is a prime number.", num);

} else {

printf("%d is not a prime number.", num);

}

return 0;

}

```

9. 字符串反转

```c

#include

#include

int main() {

char str[100];

printf("Enter a string: ");

gets(str);

strrev(str);

printf("Reversed string: %sn", str);

return 0;

}

```

10. 数组求和

```c

#include

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

int sum = 0, i;

for(i = 0; i < n; ++i) {

sum += arr[i];

}

printf("Sum of array elements: %dn", sum);

return 0;

}

```

...(中间省略部分内容)

这是其中的10个例子,涵盖了C语言的输入输出、条件语句、循环语句、函数、数组等多个方面。希望这些例子能对你的C语言学习和编程实践有所帮助。通过练习这些例子,你可以逐渐掌握C语言的基本语法和编程技巧,提高自己的编程水平。祝你学习顺利!


本文标签: C语言 编程 例子 帮助 语句