Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:03:56+00:00 2026-05-28T06:03:56+00:00

I am using Dev-C++. It doesn’t show any code error, but fails to work.

  • 0

I am using Dev-C++. It doesn’t show any code error, but fails to work.

It works when I try small numbers like 10 or 20

I am working on this 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.

    #include<stdio.h>
#include<stdlib.h>
int main()
{
const int N=100;
int a=1,b=2,i,t[N],S=0,c,j;
t[0]=1;
t[1]=2;
for(i=2;i<N;i++){
t[i]=t[i-2]+t[i-1];
if(t[i]>4000000)
{
for(j=1;j<=i-1;j++){
                    c=t[j]%2;
                    if(c==0){
                             S=S+t[j];
                            }
                    else    {
                             continue;
                    }}
break;
}
}
printf("%d\n",S);
system("pause");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:03:56+00:00Added an answer on May 28, 2026 at 6:03 am

    You don’t need an array to store all those numbers, you can get away with storing the last two terms in the sequence, since that’s all that’s need to calculate the next term.

    Trying to allocate that much space on the stack is asking for trouble since the stack is a relatively limited resource.

    In fact, that exact code entered into gcc on a Linux box gives me a segmentation violation when I try to run it, for precisely that reason.

    On top of that, your code is not getting the even valued terms, it’s getting every term, and you’re getting the first four million values, rather than the values below four million which was specified.

    The sort of code you’re after would look like this:

    #include <stdio.h>
    
    int main (void) {
        // Accumulator and terms (acc is zero because first two terms are odd).
    
        int acc = 0, t1 = 1, t2 = 1, t3;
    
        // Continue until next term is 4mil or more.
    
        while ((t3 = t1 + t2) < 4000000) {
            // printf ("DEBUG: %d %d %d %s\n", t1, t2, t3,
            //     ((t3 % 2) == 0) ? "<<" : "");
    
            // Accumulate only even terms.
    
            if ((t3 % 2) == 0) acc += t3;
    
            // Cycle through terms.
    
            t1 = t2; t2 = t3;
        }
    
        // Print the accumulated value.
    
        printf ("%d\n", acc);
    
        return 0;
    }
    

    And the output:

    4613732
    

    If you test that program by un-commenting the debug statement, you see:

    DEBUG: 1 1 2 <<
    DEBUG: 1 2 3 
    DEBUG: 2 3 5 
    DEBUG: 3 5 8 <<
    DEBUG: 5 8 13 
    DEBUG: 8 13 21 
    DEBUG: 13 21 34 <<
    DEBUG: 21 34 55 
    DEBUG: 34 55 89 
    DEBUG: 55 89 144 <<
    DEBUG: 89 144 233 
    DEBUG: 144 233 377 
    DEBUG: 233 377 610 <<
    DEBUG: 377 610 987 
    DEBUG: 610 987 1597 
    DEBUG: 987 1597 2584 <<
    DEBUG: 1597 2584 4181 
    DEBUG: 2584 4181 6765 
    DEBUG: 4181 6765 10946 <<
    DEBUG: 6765 10946 17711 
    DEBUG: 10946 17711 28657 
    DEBUG: 17711 28657 46368 <<
    DEBUG: 28657 46368 75025 
    DEBUG: 46368 75025 121393 
    DEBUG: 75025 121393 196418 <<
    DEBUG: 121393 196418 317811 
    DEBUG: 196418 317811 514229 
    DEBUG: 317811 514229 832040 <<
    DEBUG: 514229 832040 1346269 
    DEBUG: 832040 1346269 2178309 
    DEBUG: 1346269 2178309 3524578 <<
    4613732
    

    and, if you add up all the even numbers at the end of those DEBUG lines, you do indeed get the given value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I code C++ using MS Dev Studio and I work from home two days
I have been using IAR so far, but it crashes sometimes and doesn't have
I'm trying to get the font-weight of a property using latest jquery, doesn't work
This bit of code used to work, and now it doesn't: var url =
In my Django project I am using Product.objects.all().order_by('order') in a view, but it doesn't
I'm using Dev-Pas 1.9.2 and am trying to make sure the program doesn't crash
I've noticed that the PHP make that I'm using on a dev box doesn't
I' try to drawing canvas using example2 taken from http://dev.opera.com/articles/view/html5-canvas-painting/ . It works on
I'm using CKEditor in a textarea, it works perfectly on dev environment, although the
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.