Jan 25, 2022

Project Euler -2 (C Language)

Even Fibonacci Sum Problem code Solution

 /* Each new term in the Fibonacci sequence is generated
by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose
 values do not exceed four million,
find the sum of the even-valued terms.

*/
#include <stdio.h>
int main()
{
    int fibbo = 0;
    int a, b, c;
    a = 1;
    b = 2;
    while (a <= 4000000)
    {
        if (a % 2 == 0)
        {
            fibbo += a;
        }
        printf("%d\n", a);
        c = b;
        b = a + b;
        a = c;
    }

    printf("SUM : %d\n", fibbo);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...