Saturday, August 31, 2013

Project Euler question 1 fizz buzz

This worked! Learnt for loops and if statements, compound cout, signed variables.
//============================================================================
// Name        : euler_q1_fizz_buzz.cpp
// Author      : mjl
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {

	//you can create variable and assign a number at the same time
	//don't forget the ; after the variable declaration

	//don't foreget the ; inside the for loop declaration BUT...
	//you don't need a ; after the last increase statememt in the loop

	//make upper_limit unsigned for the hell of it
	unsigned int upper_limit = 1000;
	int answer = 0;

	// % and == same as python for modularity and comparison

	for (int x=1; x < upper_limit; x++) {
		if (x % 3 == 0){
			answer += x;
			continue;
		}
		if (x % 5 == 0){
			answer += x;
		}
	}

	//use a composite cout statement for the hell of it
	cout << "the answer is " << answer;

	return 0;
}

No comments:

Post a Comment