Skip to content

Instantly share code, notes, and snippets.

@ArtBears
Created May 2, 2013 02:29
Show Gist options
  • Save ArtBears/5499798 to your computer and use it in GitHub Desktop.
Save ArtBears/5499798 to your computer and use it in GitHub Desktop.
Made the first one as anonymous, sorry! This is a program is my solution to a Google c++ problem with the following bounds: This is a number analogy to a famous card trick. Ask the user to enter a three-digit number. Think of the number as ABC (where A, B, C are the three digits of the number). Now, find the remainders of the numbers formed by A…
#include <iostream>
using namespace std;
int firstNumber(int x) // from left to right. Finds the number in the hundreds place.
{
int p, q;
p = x % 100;
q = ((x - p) / 100);
return q;
}
int secondNumber(int x) // finds the number in the tens place
{
int p;
p = ((x % 100) * 10) / 100;
return p;
}
int thirdNumber(int x) // finds the number in the ones place
{
int p;
p = x % 10;
return p;
}
int findRemainder(int hundreds, int tens, int ones) // finds the remainder of the different Number orders entered and checks // for factors of 11
{
int newNumber;
hundreds = hundreds * 100;
tens = tens * 10;
newNumber = (hundreds + tens + ones) % 11;
return newNumber;
}
bool isOdd(int x) // checks to see if the number is odd or even
{
if((x % 2) == 0)
return false;
else
return true;
}
int lessThan20(int x) // Checks that the Number entered is less than 20 or equal to 9 and adds or substracts 11.
{
if(x < 9)
x = (x + 11) / 2;
else if (x > 9)
x = (x - 11) / 2;
else if(x == 9)
return x;
return x;
}
int equation( int X, int Y, int Z) // a function to solve the equation after splitting the number in 3
{
int U,P,S; // created ups because I don't want to get the variable values confused
U = X + Y;
P = Y + Z;
S = Z + X;
if( isOdd(U) == true)
{
U = lessThan20(U);
}
else
U = U / 2;
if( isOdd(P) == true)
{
P = lessThan20(P);
}
else
P = P / 2;
if( isOdd(S) == true)
{
S = lessThan20(S);
}
else
S = S / 2;
cout << "A: " << U << endl;
cout << "B: " << P << endl;
cout << "C: " << S << endl;
return 3;
}
int main() {
int ABC, A, B, C, X, Y, Z;
cout << "Enter a 3 digit Number: ";
cin >> ABC;
if((ABC > 99) && (ABC < 1000)) // Error checking. Makes sure the user enters a 3 digit number
{
A = firstNumber(ABC);
B = secondNumber(ABC);
C = thirdNumber(ABC);
X = findRemainder(A, B, C);
Y = findRemainder(B, C, A);
Z = findRemainder(C, A, B);
equation(X, Y, Z);
return 1;
}
else
{
cout << "The number must be 3 digits!" << endl;
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment