#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); }的正确C语言程序
时间: 2024-01-17 07:05:04 浏览: 50
这是一个修正过的程序:
```c
#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!=%ld\n", n, x);
return 0;
}
```
修正了以下问题:
1. 删除了函数头中的分号;
2. 修正了 `else if (n==0 && n==1)` 为 `else if (n==0 || n==1)`,因为当 `n` 为 0 或 1 时,阶乘为 1;
3. 修正了 `scanf("%d", n)` 为 `scanf("%d", &n)`,因为 `scanf` 需要传入变量的地址;
4. 在 `main` 函数中增加了 `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) //函数声明与定义应该在同一行,去掉多余的分号
{
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; //增加返回值
}
下面的程序是求n!,请改错: #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); }
代码中的函数定义缺少函数名,需要在函数定义前加上函数名 Factorial。同时,scanf函数需要传入n的地址,应该为&n。
修改后的代码如下:
```
#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!=%ld\n", n, x);
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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)