Skip to content

Instantly share code, notes, and snippets.

@mshafae
Last active February 25, 2025 04:42
Show Gist options
  • Save mshafae/978da2ef9ca32695d1ac0396c1427feb to your computer and use it in GitHub Desktop.
Save mshafae/978da2ef9ca32695d1ac0396c1427feb to your computer and use it in GitHub Desktop.
CPSC 120 Check to see if a number is even or not
// Gist https://gist.github.com/mshafae/978da2ef9ca32695d1ac0396c1427feb
// Filename cpsc120_is_even.cc
// CompileCommand clang++ -std=c++20 cpsc120_is_even.cc
// Check to see if a number is even or not
#include <iostream>
// Poor style
// bool IsEven(int number) {
// if ((number % 2) == 0) {
// return true;
// } else {
// return false;
// }
// }
// Better style
// bool IsEven(int number) {
// bool answer{false};
// answer = (number % 2) == 0;
// return answer;
// }
// Best style
bool IsEven(int number) {
return (number % 2) == 0;
}
int main(int argc, char const* argv[]) {
int number{7};
if (IsEven(number)) {
std::cout << "it is even.\n";
} else {
std::cout << "it is not even.\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment