Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What 's wrong with this fibonacci function?

0 votes
asked Mar 17, 2018 by anonymous
#include <stdio.h>
int fib(int a){
    if(a==0)
        return 0;
    else if(a==1)
        return 1;
    else
        return fib(a) = fib(a-1) + fib(a-2);
}

int main(){
    int n=0;
    printf("type: ");
    scanf("%d", &n);
    printf("%d", fib(n));

    return 0;
}

2 Answers

0 votes
answered Mar 17, 2018 by Joao

try this:

return fib(a-1) + fib(a-2);

instead of:

return fib(a) = fib(a-1) + fib(a-2);

0 votes
answered Mar 17, 2018 by smit zadafiya (220 points)
#include <stdio.h>
int fib(int a){
    int nextterm,t1=0,t2=1;
 for(int i = 1;i<=a;++i)
 {
    printf("\nfibonacci series :  ");
    printf("%d",t1);
    nextterm=t1+t2;
    t1=t2;
    t2=nextterm;
 }
}

void main(){
    int n;
    printf("type: ");
    scanf("%d", &n);
    printf("%d", fib(n));

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...