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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:53:44+00:00 2026-05-22T21:53:44+00:00

I’m learning programming with Java. This is my solution to the second problem in

  • 0

I’m learning programming with Java. This is my solution to the second problem in Project Euler which seems to give the right answer, however, I’m sure it’s buggy.

The problem is if I change the MAX_TERM_VALUE to 100, I get the answer 188. By calculating the answer by hand, I’m expecting 44. It seems that the program loops one to many times but I can’t work out how to stop it doing this.

Thanks in advance.

public static void main(String[] args) {
    int fibA = 0;
    int fibB = 1;
    int fibC = 0;
    int total = 0;

    while (fibC < MAX_TERM_VALUE) {
        fibC = fibA + fibB;
        fibA = fibB;
        fibB = fibC;

        if (fibC %2 == 0) {
            total = total + fibC;
        }
    } 
    System.out.println(total);  
}
private static final int MAX_TERM_VALUE = 4000000;
  • 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-22T21:53:45+00:00Added an answer on May 22, 2026 at 9:53 pm

    The reason for error is that you only check that a number is valid after summing up, thus you also get 134 as it first gets over MAX_TERM_VALUE the next iteration.

    Look:

    public static void main(String[] args) {
        int fibA = 0;
        int fibB = 1;
        int fibC = 0;
        int total = 0;
    
        while (fibC < MAX_TERM_VALUE) { //fibC is 89 and under MAX_TERM_VALUE = 100
            fibC = fibA + fibB; //fibC is 144 and more than MAX_TERM_VALUE 
            fibA = fibB;
            fibB = fibC;
    
            if (fibC %2 == 0) {
                total = total + fibC; //The fibC is added anyway as the check only happens next iteration
            }
        } 
        System.out.println(total);  
    }
    private static final int MAX_TERM_VALUE = 4000000;  
    

    You should do the following instead:

    public static void main(String[] args) {
        int fibA = 0;
        int fibB = 1;
        int fibC = 0;
        int total = 0;
    
        while (true) { 
            fibC = fibA + fibB;  
            fibA = fibB;
            fibB = fibC;
            if (fibC >= MAX_TERM_VALUE){
                 break;
            }
            if (fibC %2 == 0) {
                total = total + fibC; 
            }
        } 
        System.out.println(total);  
    }
    
    private static final int MAX_TERM_VALUE = 4000000;  
    

    Which should work as the check is done before summing up.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.