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

  • SEARCH
  • Home
  • 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 8291467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:08:23+00:00 2026-06-08T13:08:23+00:00

I am working on Project Euler, the first problem, and I’ve gotten this program

  • 0

I am working on Project Euler, the first problem, and I’ve gotten this program to output the numbers I need, but I cannot figure out how to take the outputted numbers and add them together.

Here’s the code:

#include <iostream>
#include <cmath>

int main(void) {

    int test = 0;

    while (test<1000) {
        test++;
            if (test%3 == 0 && test%5 == 0) {

                std::cout << test << std::endl;

            }
    }

    std::cin.get();

    return 0;
}
  • 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-06-08T13:08:25+00:00Added an answer on June 8, 2026 at 1:08 pm

    The easiest option would be a total variable that you add to as the criteria match.

    The first step is creating it and initializing it to 0, so you end up with the right number later.

    int total = 0;
    

    After that, subtotals are added to it, so that it accumulates the overall total.

    total += 5;
    ...
    total += 2;
    //the two subtotals result in total being 7; no intermediate printing needed
    

    Once you’ve added on the subtotals, you can just print it as the overall total.

    std::cout << total;
    

    Now, here’s how it fits into the code at hand, along with some other pointers:

    #include <iostream>
    #include <cmath> //<-- you're not using anything in here, so get rid of it
    
    int main() {
        int test = 0;
        int total = 0; //step 1; don't forget to initialize it to 0
    
        while (test<1000) { //consider a for loop instead
            test++;
    
            if (test % 3 == 0 && test % 5 == 0) {
                //std::cout << test << std::endl;
                total += test; //step 2; replace above with this to add subtotals
            }
        }
    
        std::cout << total << std::endl; //step 3; now we just output the grand total
    
        std::cin.get();    
        return 0; //this is implicit in c++ if not provided
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I'm again working on Project Euler, this time problem #4. The point of this
I am working on Project Euler Problem 4 , and need to find the
I'm working on solving the Project Euler problem 25: What is the first term
I'm working on problem four of Project Euler and am running into a stackoverflow
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet
I'll try to be concise this time around! I'm still working Project Euler, this
Once again working on Project Euler, this time my script just hangs there. I'm
I am working on Problem 14 on Project Euler, and my code seems to
I'm working on a program for Project Euler to add all the digits of

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.