用C语言编写一个函数求通项为{1/nn..nn}(分母有n个n
时间: 2023-11-22 08:54:44 浏览: 68
以下是用C语言编写的求通项为{1/nn..nn}的函数:
```c
#include <stdio.h>
double sequence(int n) {
double term = 1.0;
for (int i = 1; i <= n; i++) {
term /= n;
}
return term;
}
int main() {
int n;
printf("请输入n:");
scanf("%d", &n);
printf("通项为:%lf\n", sequence(n));
return 0;
}
```
该函数使用了循环结构,通过连续除以n得到通项的值。输入n后,即可输出通项的值。需要注意的是,当n较大时,计算结果可能会出现精度误差。
相关问题
利用C语言求和s=1+1/22+1/333+...+1/nn...nn+.....
以下是利用C语言求和s=1+1/22+1/333+...+1/nn...nn+.....的代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("请输入n:");
scanf("%d", &n);
double s = 1.0; // 初始化s为1
for (int i = 2; i <= n; i++) {
int nn = i; // nn表示n...n,初始值为i
int count = 1; // 统计n的位数
while (nn >= 10) {
nn /= 10;
count++;
}
nn = i; // 重新赋值nn为i
double val = 0.0; // 计算1/nn...nn的值
for (int j = 1; j <= count; j++) {
val += nn / pow(10, (count-j+1));
}
s += 1.0 / val; // 累加到s中
}
printf("s = %lf\n", s);
return 0;
}
```
该程序首先读入一个正整数n,然后使用for循环遍历2到n的每个整数。对于每个整数i,我们需要计算1/nn...nn的值,其中nn表示n...n(共有i位)。为了计算nn,我们需要先统计n的位数,然后根据位数分解出每一位上的值。最后,利用pow函数计算1/nn...nn的值,并将其累加到s中。最终输出s的值即可。
使用函数编写求a+aa+aaa...+n个a的值,c语言
以下是使用函数编写求a+aa+aaa...+n个a的值的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
int sum(int a, int n);
int main() {
int a, n;
printf("请输入两个整数a和n:");
scanf("%d%d", &a, &n);
printf("a+aa+aaa+...=%d", sum(a, n));
return 0;
}
int sum(int a, int n) {
int cn = 0, s = 0, i;
for (i = 1; i <= n; i++) {
cn = cn + a;
s = s + cn;
a = a * 10;
}
return s;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)