CSC 240

Theory of Computation

Lab 01 - Computational Math

Write C++ code to solve the following problems:

Note that your program output for all of the problems below should match mine exactly. Take special note of edge cases and special situations.

Your programs should also work for all cases described in the problem, not just the example cases shown.

1. All Multiples of n

Write a program that prompts the user for a multiple, and an upper limit, then prints out the sum of all the multiples of that number between 1 and the upper limit (inclusive). Your program should work for any integer multiple and limit.

Here are some example runs of such a program:

Enter a multiple: 4
Enter limit: 1000

The sum of all multiples of 4 between 1 and 1000 is: 125500
Enter a multiple: 3
Enter limit: 487

The sum of all multiples of 3 between 1 and 487 is: 39609

Everything you learned in Java about for loops and variable scoping still applies in C++.

Here are some references that might come in handy:

2. What time is it Mr. Fox?

Modular Arithmetic is useful in many situations where we want to divide something into discrete categories.

In C++ the modulus operator is %. So, to determine if a number is even or odd in C++, we could use a program like this:

#include <iostream>

using namespace std;

int main() 
{
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if(number % 2 == 0) {
        cout << "That number is even!" << endl;
    }
    else {
        cout << "That number is odd!" << endl;
    }
}

Running the program a couple of times, we get:

Enter a number: 999
That number is odd!
Enter a number: 12
That number is even!

Using the modulus operator, write a program in C++ that asks the user for the current hour on a standard 12 hour clock. Then ask them for the number of hours to move forward, then tell them the hour on a standard 12 hour cloud that it will be at that time.

Here are a couple of example runs of such a program:

What hour is it (1 - 12): 12
Hours to move forward: 3

In 3 hours it will be 3:00. 
What hour is it (1 - 12): 5
Hours to move forward: 700

In 700 hours it will be 9:00. 
What hour is it (1 - 12): 11
Hours to move forward: 1

In 1 hour it will be 12:00. 

Note that your output should match mine exactly, so make sure you don't print 0 o'clock for the last case. Also note the output for the last case says 1 hour, not 1 hours.

3. Leap Year

Write a program that asks the user for a year, and then tells them if that year is a leap year. Note that contrary to popular belief, leap year is not every four years.

You can find the rules for determining if a year is a leap year on Wikipedia:

Here are a couple of example runs of the program:

Enter a year: 2000
That year is a leap year!
Enter a year: 1915
That year was not a leap year!

Note that the modulus operator will be useful for this program.

Submission

You should create three different .cpp files for this assignment. Put all three files in a zip file, and submit the zip to mySVU.