改正代码#include <stdio.h> unsigned long Factorial(unsigned int n); { if (n < 0) { printf("data error!"); return 0; } else if (n==0 && n==1) { return 1; } else { return n * Factorial(n-1); } } main() { int n; unsigned long x; printf("Input n:\n"); scanf("%d", n); x = Factorial(n); printf("%d!=%ld\n", n, x); }
时间: 2024-03-18 21:44:25 浏览: 40
#include <stdio.h>
unsigned long Factorial(unsigned int n) //函数声明与定义应该在同一行,去掉多余的分号
{
if (n < 0)
{
printf("data error!");
return 0;
}
else if (n == 0 || n == 1) //修改判断条件,应该使用||而不是&&
{
return 1;
}
else
{
return n * Factorial(n - 1); //递归调用时,参数应该为n-1
}
}
int main() //函数类型应该为int,增加返回值
{
unsigned int n; //将n的类型修改为unsigned int
unsigned long x;
printf("Input n:\n");
scanf("%u", &n); //修改读入格式符,加入&符号
x = Factorial(n);
printf("%u!=%ld\n", n, x); //修改格式符,将%u和%ld对应到n和x上
return 0; //增加返回值
}
相关问题
#include <stdio.h> unsigned long Factorial(unsigned int n); { if (n < 0) { printf("data error!"); return 0; } else if (n==0 && n==1) { return 1; } else { return n * Factorial(n-1); } } main() { int n; unsigned long x; printf("Input n:\n"); scanf("%d", n); x = Factorial(n); printf("%d!=%ld\n", n, x); }
这段代码有一些问题,可能是因为复制粘贴时出现了误差,我来帮你指出一下:
1. 函数定义时应该加上返回值类型,这里应该加上 unsigned long。
2. 函数定义时没有添加函数名,应该在 unsigned long 后面添加函数名 Factorial。
3. 函数定义时应该在左花括号 { 前面换行。
4. 函数中判断 n 是否等于 0 或 1 的条件应该用 || 连接,而不是 && 连接。
5. 在 main 函数中,应该在 scanf 函数的第二个参数前面加上 & 符号,表示传递变量的地址。
下面是修改后的代码:
```
#include <stdio.h>
unsigned long Factorial(unsigned int n)
{
if (n < 0)
{
printf("data error!");
return 0;
}
else if (n == 0 || n == 1)
{
return 1;
}
else
{
return n * Factorial(n-1);
}
}
int main()
{
int n;
unsigned long x;
printf("Input n:\n");
scanf("%d", &n);
x = Factorial(n);
printf("%d!=%lu\n", n, x);
return 0;
}
```
希望这个回答能够帮到你!
改错:#include <stdio.h> unsigned long Factorial(unsigned int n); { if (n < 0) { printf("data error!"); return 0; } else if (n==0 && n==1) { return 1; } else { return n * Factorial(n-1); } } main() { int n; unsigned long x; printf("Input n:\n"); scanf("%d", n); x = Factorial(n); printf("%d!=%ld\n", n, x); }
以下是修改后的程序:
```
#include <stdio.h>
unsigned long Factorial(unsigned int n) { // 修正1:移动函数体外部,去掉多余的{
if (n < 0) {
printf("data error!");
return 0;
} else if (n == 0 || n == 1) { // 修正2:修改判断条件
return 1;
} else {
return n * Factorial(n-1);
}
}
int main() { // 修正3:添加返回类型
int n;
unsigned long x;
printf("Input n:\n");
scanf("%d", &n); // 修正4:需要传入n的地址
x = Factorial(n);
printf("%d!=%ld\n", n, x);
return 0; // 修正5:添加返回语句
}
```
修改说明:
1. 函数体应该在函数声明后面,去掉多余的`{`;
2. 第二个判断条件应该是`n == 0 || n == 1`;
3. `main`函数需要添加返回类型`int`;
4. `scanf`函数需要传入`n`的地址,即`&n`;
5. `main`函数需要添加返回语句`return 0`。
这样修改后,程序才能正确运行。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)