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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:40:58+00:00 2026-05-11T06:40:58+00:00

So I’m learning C++. I’ve got my C++ Programming Language and Effective C++ out

  • 0

So I’m learning C++. I’ve got my ‘C++ Programming Language’ and ‘Effective C++’ out and I’m running through Project Euler. Problem 1…dunzo. Problem 2…not so much. I’m working in VS2008 on a Win32 Console App.

Whats the Sum of all even terms of the Fibonacci Sequence under 4 million?

It wasn’t working so I cut down to a test case of 100…

Here’s what I wrote…

// Problem2.cpp : Defines the entry point for the console application. //  #include 'stdafx.h' using namespace std;  int _tmain(int argc, _TCHAR* argv[]) {     cout << 'Project Euler Problem 2:\n\n';     cout << '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:\n\n';     cout << '1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n\n';     cout << 'Find the sum of all the even-valued terms in the sequence which do not exceed four million.\n\n';     cout << 'Answer:  ' << Solve(); }  double Solve() {     int FibIndex = 0;     double result = 0.0;     double currentFib = GenerateNthFibonacciNumber(FibIndex);     while (currentFib < 100.0){         cout << currentFib << ' ' << (int)currentFib << ' ' << (int)currentFib % 2 << '\n';         if ((int)currentFib % 2 == 0){             result += currentFib;             cout<<(int)currentFib;         }         currentFib = GenerateNthFibonacciNumber(++FibIndex);     }     return result; }  double GenerateNthFibonacciNumber(const int n){     //This generates the nth Fibonacci Number using Binet's Formula     const double PHI = (1.0 + sqrt(5.0)) / 2.0;     return ((pow(PHI,n)-pow(-1.0/PHI,n)) / sqrt(5.0)); } 

And here’s the output…

Project Euler Problem 2:

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, …

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

0 0 0
1 1 1
1 1 1
2 2 0
3 3 1
5 5 1
8 8 0
13 13 1
21 21 1
34 34 0
55 54 0
89 89 1
Answer: 99

So I have three columns of debug code…the number returned from the generate function, (int)generatedNumber, and (int)generatedNumber % 2

So on the 11th term we have

55,54,0

Why does (int)55 = 54?

  • 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. 2026-05-11T06:40:59+00:00Added an answer on May 11, 2026 at 6:40 am

    Casting to int truncates the number – same as if you’d called floor(currentFib). So even if currentFib is 54.999999… (a number so close to 55 that it will be rounded up when printed), (int)currentFib will produce 54.

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If you have the four digit strings (1200, 1300) and… May 11, 2026 at 11:47 am
  • added an answer Instance methods take instance as first argument: class A :… May 11, 2026 at 11:47 am
  • added an answer In pg, it's up to you to track trigger recursion.… May 11, 2026 at 11:46 am

Related Questions

So I'm getting a new job working with databases (Microsoft SQL Server to be
So I have a Sybase stored proc that takes 1 parameter that's a comma
So I'm embarking on an ASP.NET MVC project and while the experience has been
So I've got a JPanel implementing MouseListener and MouseMotionListener : import javax.swing.*; import java.awt.*;
So I wrote some perl that would parse results returned from the Amazon Web
So I just fixed a bug in a framework I'm developing. The pseudo-pseudocode looks
So I'm not quite convinced about OpenID yet, and here is why: I already
So I was listening to the latest Stackoverflow podcast ( episode 19 ), and
So I am writing a registration form and I need the display name to
So I've looked at this use of the freebase API and I was really

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.