首页 百科 正文

c语言编程代码大全

百科 编辑:沅恒 日期:2024-04-23 12:46:47 657人浏览

C语言编程笔试题

1.

编写一个程序,将两个整数相加并输出结果。

```c

include

int main() {

int num1, num2, sum;

printf("Enter first integer: ");

scanf("%d", &num1);

printf("Enter second integer: ");

scanf("%d", &num2);

sum = num1 num2;

printf("Sum: %d\n", sum);

return 0;

}

```

2.

编写一个程序,计算并输出给定整数的阶乘。

```c

include

int main() {

int num, factorial = 1;

printf("Enter an integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Error: Factorial of negative number is undefined.\n");

return 1;

}

for (int i = 1; i <= num; i) {

factorial *= i;

}

printf("Factorial of %d is: %d\n", num, factorial);

return 0;

}

```

3.

编写一个程序,查找给定数组中的最大值和最小值,并输出它们的位置。

```c

include

define SIZE 10

int main() {

int arr[SIZE] = {10, 5, 8, 20, 15, 3, 12, 18, 7, 25};

c语言编程代码大全

int max_index = 0, min_index = 0;

for (int i = 1; i < SIZE; i) {

if (arr[i] > arr[max_index])

max_index = i;

if (arr[i] < arr[min_index])

min_index = i;

}

printf("Maximum value %d found at index %d.\n", arr[max_index], max_index);

printf("Minimum value %d found at index %d.\n", arr[min_index], min_index);

return 0;

}

```

这些题目涵盖了基本的输入输出、条件语句、循环语句和数组操作。希望它们对你有帮助!

分享到

文章已关闭评论!