I'm using the following code to do some addition and subtraction, mod 12. [Maybe I'm using the wrong mathematical term, but this does not seem to correspond to the "modulus" operator (%x), which calculates the remainder when divided by x.] Think of addition as moving clockwise on a clock face, and subtraction as moving counter-clockwise. 5+8=1, 5-8=9...
//This program adds and substracts two integers together, mod12.
#include <iostream>
using namespace std;
main ()
{
int x;
int y;
int sum;
int difference;
cout << "Enter an integer, 0-11: " << endl;
cin >> x;
cout << "Enter another integer, 0-11: " << endl;
cin >> y;
sum = x+y;
if (sum > 11) // "corrects" for mod12 addition
{
sum = sum - 12;
}
difference = x-y;
if (difference < 0) // "corrects" for mod12 subtraction
{
difference = difference + 12;
}
cout << x << "+" << y << "=" << sum << endl;
cout << x << "-" << y << "=" << difference;
return(0);
}
My question is this:
Is there a better way to do this than by using the "if" statements I've used here?
Thanks for the help! I don't get it -- I swear that I tried those very equations and didn't get correct answers, but today it works. I must have typed something wrong, thought I'm not sure what.