Thread: Fibonacci

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    16

    Post Fibonacci

    prob-B.pdf

    And this is my code that related to the question :
    Code:
    #include <stdio.h>
    
    
    typedef unsigned long long F;
    
    
    F fibonacci(F n);
    
    
    int main()
    {
        int n, i, j;
        
        printf("--- Fibonacci series program ---\n");
        printf("Enter a number:");
        scanf("%d",&n);
        
        printf("\nFibonacci series:\n");
        j = 0;
        for ( i = 1 ; i <= n ; i++ )
        {
            printf("%d ", fibonacci(j));
            j++;
        }
    }
    
    
    F fibonacci(F n)
    {
        int i;
        F f1 = 0;
        F f2 = 1;
        F fi;
        
        if(n == 0)
            return 0;
        if(n == 1)
            return 1;
        
        for(i = 2 ; i <= n ; i++ )
        {
            fi = f1 + f2;
            f1 = f2;
            f2 = fi;
        }
        return fi;
    }
    Did i coded right, or if there is something wrong please help me.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,627
    fibonacci() returns an unsigned long long, but the printf is using "%d", which is for a signed int. For Visual Studio, the printf would use "%llu" for an unsigned long long.
    Last edited by rcgldr; 1 Week Ago at 08:22 AM.

  3. #3
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    103
    Quote Originally Posted by Audy Naufal View Post
    prob-B.pdf

    And this is my code that related to the question :
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n, i, j;
        
        printf("--- Fibonacci series program ---\n");
        printf("Enter a number:");
        scanf("%d",&n);
        
        printf("\nFibonacci series:\n");
        j = 0;
        for ( i = 1 ; i <= n ; i++ )
        {
            printf("%d ", fibonacci(j));
            j++;
        }
    }
    The program run almost correct; "j" is redundant.

    Code:
    int main(void)
    {
        int n;  //i -> NO!
        
        printf("--- Fibonacci series program ---\n");
        printf("Enter a number:");
        scanf("%d",&n);
        
        printf("\nFibonacci series:\n");
        //Start with '0' else for example: n = 6 -> solution = 5, that is wrong
        for (int i = 0 ; i <= n ; i++ )
        {
            printf("%d ", fibonacci(i));        
        }
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci using do-while
    By luizflleitao in forum C Programming
    Replies: 1
    Last Post: 09-25-2016, 01:18 AM
  2. Fibonacci
    By Pookie in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2011, 01:38 AM
  3. nth fibonacci
    By meriororen in forum C Programming
    Replies: 1
    Last Post: 08-10-2009, 05:13 AM
  4. sum of fibonacci
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 09:28 AM
  5. fibonacci(10,000)
    By blindman858 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2005, 12:03 AM

Tags for this Thread