gpt4 book ai didi

c - 斐波那契数列存在一些运行时错误

转载 作者:行者123 更新时间:2023-11-30 20:08:51 26 4
gpt4 key购买 nike

[在此处输入图像描述][1][此处当我在 if(series==1) return 之前在斐波那契定义函数中采用 if(series==0) return 0; 时1; 然后斐波那契数列打印成功,但是当我删除这一行然后编译我的程序时,它需要 term inout 然后显示一条错误消息,如

Fibonacci.exe has stop working but when I add that line program executing successfully. what happening that. somebody will tell me ][2] here's my code :

    #include<stdio.h>
int fibonacci(int); // declaration of fibonacci function.

int main(){
int input_num,i,series=1;
printf("Enter the term you want to print fibonacci series : ");
scanf("%d", &input_num);

printf("\n Fibonacci Series Are : ");

for(i=0; i<input_num; i++){
printf("%d,", fibonacci(series));
series++;
}

return 0;
}

//defination of fibonacci function
int fibonacci(int series){


if(series==1) return 1;

return(fibonacci(series-1)+fibonacci(series-2));

}

最佳答案

if (series==1) return 1;

这存在一个问题,您还使用 series-2 调用该函数,因此它可能会小于 1。替换为:

if (series<=1) return 1;

另请参阅“防御性编程”

关于c - 斐波那契数列存在一些运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com