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 4624878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:11:33+00:00 2026-05-22T03:11:33+00:00

I’m a relatively new java programmer and I’ve been tinkering around with this program

  • 0

I’m a relatively new java programmer and I’ve been tinkering around with this program for the better part of the day now and I’m still stuck; I was hoping that you could help me with this.

So the program is supposed to meet the following requirements:

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.

This is my code:

    //Generates Fibonacci sequence
    while (fibNum < 144)
    {
        int lastValue = (Integer) fibList.get(fibList.size()-1);
        int secondToLastValue = (Integer) fibList.get(fibList.size()-2);

        fibNum = secondToLastValue + lastValue;

        if (fibNum < 144)
        {
            fibList.add(fibNum);
        }

    //Picks out the even numbers from limitFibList
    for (int i = 0; i < fibList.size(); i++)
    {
        if ((Integer) fibList.get(i) % 2 == 0)
        {
            evenNumsFibList.add(fibList.get(i));
        }
    }

    //Sums up the total value of the numbers in the evenNumsFibList
    for (int i = 0; i < evenNumsFibList.size(); i++)
    {
        sum += (Integer) evenNumsFibList.get(i); 
    }

…and this is the output that I’m getting:

Fibonacci sequence list: [1, 2, 3]
Size of the Fibonacci list: 3
Even Numbers list: [2]
Total sum of even numbers: 2

Fibonacci sequence list: [1, 2, 3, 5]
Size of the Fibonacci list: 4
Even Numbers list: [2, 2]
Total sum of even numbers: 6

Fibonacci sequence list: [1, 2, 3, 5, 8]
Size of the Fibonacci list: 5
Even Numbers list: [2, 2, 2, 8]
Total sum of even numbers: 20

Fibonacci sequence list: [1, 2, 3, 5, 8, 13]
Size of the Fibonacci list: 6
Even Numbers list: [2, 2, 2, 8, 2, 8]
Total sum of even numbers: 44

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21]
Size of the Fibonacci list: 7
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8]
Total sum of even numbers: 78

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34]
Size of the Fibonacci list: 8
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34]
Total sum of even numbers: 156

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55]
Size of the Fibonacci list: 9
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 278

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 444

Fibonacci sequence list: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Size of the Fibonacci list: 10
Even Numbers list: [2, 2, 2, 8, 2, 8, 2, 8, 2, 8, 34, 2, 8, 34, 2, 8, 34, 2, 8, 34]
Total sum of even numbers: 654

Obviously my while loop is contributing to my problems, but I don’t know how to fix it.

Would greatly appreciate your help,

Haque

  • 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-22T03:11:34+00:00Added an answer on May 22, 2026 at 3:11 am

    Looks like you are missing the close brackets on the while loop. So the other for‘s are running within it.

    So:

    while (fibNum < 144)
        {
            int lastValue = (Integer) fibList.get(fibList.size()-1);
            int secondToLastValue = (Integer) fibList.get(fibList.size()-2);
    
            fibNum = secondToLastValue + lastValue;
    
            if (fibNum < 144)
            {
                fibList.add(fibNum);
            }
        }
    
        //Picks out the even numbers from limitFibList
        for (int i = 0; i < fibList.size(); i++)
        {...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.