请给我完整的c语言代码
时间: 2024-03-09 15:48:22 浏览: 31
好的,以下是完整的C语言代码:
```c
#include <stdio.h>
int fun(unsigned int s) {
int res = 0, base = 1;
while (s > 0) {
int digit = s % 10;
s /= 10;
if (digit % 2 == 0) {
res += digit * base;
base *= 10;
}
}
return res;
}
int main() {
unsigned int s;
printf("Please input an integer:\n");
scanf("%u", &s);
printf("The result is: %d\n", fun(s));
return 0;
}
```
运行程序后,会提示您输入一个整数,然后调用`fun`函数并输出结果。
相关问题
请给我完整的C语言代码
好的,以下是完整的C语言代码:
```c
#include <stdio.h>
int fun(unsigned int s) {
int res = 0, base = 1;
while (s > 0) {
int digit = s % 10;
s /= 10;
if (digit % 2 == 0) {
res += digit * base;
base *= 10;
}
}
return res;
}
int main() {
unsigned int s;
printf("Please input an integer:\n");
scanf("%u", &s);
printf("The result is: %d\n", fun(s));
return 0;
}
```
运行程序后,会提示您输入一个整数,然后调用`fun`函数并输出结果。
请给出完整的C语言代码
好的,下面是一个完整的C语言程序,它实现了在数组中存入一串数字,并用选择法对数组中10个整数按由小到大排序:
```c
#include <stdio.h>
// 选择排序函数
void selection_sort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
int main() {
int arr[20];
int i;
// 从用户输入中读入数组元素
printf("请输入10个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
// 对数组进行排序
selection_sort(arr, 10);
// 输出排序后的数组
printf("排序后的数组:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这个程序中,我们首先定义了一个 `selection_sort` 函数,它实现了选择排序算法。然后在 `main` 函数中,我们声明了一个长度为20的整型数组 `arr`,并从用户输入中读入了10个整数存入这个数组中。接着,我们调用 `selection_sort` 函数对数组进行排序,并输出排序后的结果。
注意,这个程序并没有对数组长度进行严格的限制,因此如果用户输入了超过10个元素,程序也可以正常运行,但是只会对前10个元素进行排序。如果需要对数组长度进行限制,可以在读入元素时进行判断。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)