Excercise in C: Fibonacci Sequence

Problem:

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.

৩টি মন্তব্য:

  1. output is 4613732
    -------------------------------

    #include
    #include
    int main()
    {
    long total=0,one=1,two=2,three=0;
    while (two <= 4000000)
    {
    three = one + two;
    if (two % 2 == 0)
    total += two;
    one = two;
    two = three;
    }
    printf("%d",total);
    return 0;
    }

    উত্তরমুছুন
  2. include
    main()
    {
    int result=0,one=3,two=5,three=0;
    while (two <= 500000)
    {
    three = one + two;
    if(two % 2 == 0)
    result += two;
    one = two;
    two = three;
    }
    printf("%d",result);
    return 0;
    }

    উত্তরমুছুন
  3. #include
    int main()
    {
    int n, first = 0, second = 1, next, c , sum=0;
    printf("Enter the number of terms\n");
    scanf("%d",&n);
    printf("First %d terms of Fibonacci series are :-\n",n);
    for ( c = 0 ; c < n ; c++ )
    {
    if ( c <= 1 ) next = c;
    else
    {
    next = first+second;
    first=second; second=next;
    }
    {
    sum=sum+1;
    }
    printf("%d\n",next);
    }
    }

    উত্তরমুছুন