Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Change 0 to 1 or 1 to 0.
- // Not allowed: conditionals, booleans, bit operators
- #include <iostream>
- #include <cctype>
- #include <string>
- using namespace std;
- // This function changes a 0 to a 1 or
- // a 1 to a 0, without using
- // conditionals, negation or bit operators
- int flip(int zero_or_one)
- {
- int flipped_number = 0; // The result of the flip.
- flipped_number = 1 - zero_or_one; // Flip the number.
- return flipped_number; // Return the result.
- }
- int main()
- {
- string one_or_zero; // User input.
- int result = 0; // Output after flip.
- cout << "Enter 1, and you will receive 0.\n"
- "Enter 0, and you will receive 1.\n"
- "Your entry: ";
- getline(cin, one_or_zero); // Get user input.
- // If the user did not enter a 1 or a 0,
- while (one_or_zero.compare("0") != 0 && one_or_zero.compare("1") != 0)
- {
- cout << "You must enter 1 or 0.\n"
- "Your entry: ";
- cin >> one_or_zero; // Get new input.
- }
- result = flip(stoi(one_or_zero)); // Flip the number.
- cout << result << '\n'; // Display the result.
- return 0; // End the program.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement