Thread: fibonacci

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    12

    fibonacci

    Hi. im trying to sum even fibonacci numbers only. ie. 0 2 2 4 6 10 16 ect.. could someone indicate where i've gone wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    
    int main (void)
    {
        int x, y;
        int second =0;
        int sum;
    
    
    
    
        printf("enter the fib number: ");
        scanf("%d", &y);
    
    
        for (x=0; x<=y; x++)
        {
    
    
            if (x%2 ==0 )
            {
    
    
                    sum = x+ second;
                    second =x;
                    x=sum;
    
    
            }
        printf("%d \n", sum);
        }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    You need to scrap that code and write a program that just prints out the fibonacci numbers. Then modify that program by adding code to sum up the even numbers.

    BTW, you should probably use long (or long long) integers since fibs get big fast. Even an unsigned 64-bit integer maxes out at the 93'rd fib.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci
    By Paged in forum C Programming
    Replies: 2
    Last Post: 12-22-2010, 04:42 PM
  2. Fibonacci
    By saria777 in forum C Programming
    Replies: 25
    Last Post: 06-16-2010, 07:26 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