编写一个c语言程序。要求: 编写一个fun函数用来求累加, 主函数主要用来定义对被调用函数进行声明,变量定义,函数调用,输出结果 等。
时间: 2024-05-10 08:15:17 浏览: 9
以下是一个求累加的C语言程序:
```c
#include <stdio.h>
int fun(int n); // 声明函数
int main() {
int n, sum;
printf("请输入一个正整数n:");
scanf("%d", &n);
sum = fun(n); // 调用函数
printf("1到%d的累加和为:%d\n", n, sum);
return 0;
}
int fun(int n) {
int i, res = 0;
for (i = 1; i <= n; i++) {
res += i;
}
return res;
}
```
程序中定义了一个名为`fun`的函数,用于求1到n的累加和。在`main`函数中,先定义了一个变量`n`用于存储用户输入的正整数,然后调用`fun`函数计算1到n的累加和,并将结果存储在变量`sum`中。最后输出累加和的结果。
相关问题
c语言/*请编写函数fun,函数的功能是:统计各年龄段的人数。n个年龄通过调用随机函
数生成并存入一维数组a中,年龄范围在18到70岁之间。要求函数通过调用输出函数print输出各年龄段的人数,年龄段为18到25,26到35,36到45,46到55,56到65,66到70。*/
下面是函数fun的实现代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
void print(int count[]) {
printf("18~25岁的人数:%d\n", count[0]);
printf("26~35岁的人数:%d\n", count[1]);
printf("36~45岁的人数:%d\n", count[2]);
printf("46~55岁的人数:%d\n", count[3]);
printf("56~65岁的人数:%d\n", count[4]);
printf("66~70岁的人数:%d\n", count[5]);
}
void fun(int a[], int n) {
int count[6] = {0};
for (int i = 0; i < n; i++) {
if (a[i] >= 18 && a[i] <= 25) {
count[0]++;
} else if (a[i] >= 26 && a[i] <= 35) {
count[1]++;
} else if (a[i] >= 36 && a[i] <= 45) {
count[2]++;
} else if (a[i] >= 46 && a[i] <= 55) {
count[3]++;
} else if (a[i] >= 56 && a[i] <= 65) {
count[4]++;
} else if (a[i] >= 66 && a[i] <= 70) {
count[5]++;
}
}
print(count);
}
int main() {
int a[N];
int n;
srand((unsigned)time(NULL)); // 初始化随机数种子
printf("请输入人数n:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
a[i] = rand() % 53 + 18; // 生成18~70岁之间的随机数
}
fun(a, n);
return 0;
}
```
函数fun首先初始化一个长度为6的计数数组count,然后遍历数组a中的每一个元素,根据其年龄范围累加相应年龄段的人数。最后调用输出函数print输出各年龄段的人数。在主函数中,首先获取人数n,然后使用随机函数生成n个18~70岁之间的随机数存入数组a中,最后调用函数fun统计各年龄段的人数并输出。
编写函数fun将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。
以下是一个简单的实现,可以将一个数字字符串转换为整数:
```c
#include <stdio.h>
int fun(char *str) {
int result = 0;
while (*str != '\0') {
result = result * 10 + (*str - '0');
str++;
}
return result;
}
int main() {
char str[] = "12345";
int num = fun(str);
printf("%d\n", num);
return 0;
}
```
该函数遍历数字字符串,将每个数字字符转换为数字并将其乘以10的n次方,其中n是数字字符位置,然后将结果累加到result变量中。最后,返回result作为整数值。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)