引言
C语言作为一种历史悠久且功能强大的编程语言,在计算机科学领域有着广泛的应用。对于初学者来说,掌握C语言的基础知识是至关重要的。本文将为你提供50个实用的程序设计例题,帮助你巩固C语言基础,提升编程能力。
例题一:输出“Hello, World!”
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
这是一个最简单的C语言程序,用于输出“Hello, World!”。
例题二:计算两个整数的和
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
这个程序可以读取用户输入的两个整数,并计算它们的和。
例题三:判断一个数是否为偶数
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
这个程序可以判断用户输入的整数是否为偶数。
例题四:计算一个数的阶乘
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
这个程序使用递归函数计算用户输入的整数的阶乘。
例题五:冒泡排序
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
这个程序使用冒泡排序算法对数组进行排序。
例题六:查找数组中的元素
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 25;
int result = linearSearch(arr, n, x);
if (result == -1) {
printf("Element is not present in array.\n");
} else {
printf("Element is present at index %d.\n", result);
}
return 0;
}
这个程序使用线性查找算法在数组中查找指定的元素。
例题七:计算字符串长度
#include <stdio.h>
#include <string.h>
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[] = "Hello, World!";
int length = stringLength(str);
printf("Length of string: %d\n", length);
return 0;
}
这个程序使用字符串遍历方法计算用户输入的字符串长度。
例题八:将十进制数转换为二进制数
#include <stdio.h>
void decimalToBinary(int n) {
if (n > 1) {
decimalToBinary(n / 2);
}
printf("%d", n % 2);
}
int main() {
int n = 13;
printf("Binary representation of %d is: ", n);
decimalToBinary(n);
printf("\n");
return 0;
}
这个程序使用递归方法将十进制数转换为二进制数。
例题九:计算两个矩阵的乘积
#include <stdio.h>
void multiplyMatrices(int a[][3], int b[][3], int result[][3], int rowsA, int colsA, int rowsB, int colsB) {
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
int main() {
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int result[3][3];
int rowsA = 3, colsA = 3, rowsB = 3, colsB = 3;
multiplyMatrices(a, b, result, rowsA, colsA, rowsB, colsB);
printf("Resultant matrix:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
这个程序使用嵌套循环计算两个矩阵的乘积。
例题十:判断一个数是否为素数
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n = 29;
if (isPrime(n)) {
printf("%d is a prime number.\n", n);
} else {
printf("%d is not a prime number.\n", n);
}
return 0;
}
这个程序使用试除法判断用户输入的整数是否为素数。
总结
以上是10个实用的C语言程序设计例题,涵盖了数组、字符串、递归、排序、查找、矩阵运算和素数判断等多个方面。通过学习和实践这些例题,相信你将更加熟练地掌握C语言基础,为未来的编程之路打下坚实的基础。
