Skip to content

Instantly share code, notes, and snippets.

@mshafae
Last active December 13, 2023 23:15
Show Gist options
  • Save mshafae/59c6e8a6a446fa7c32b16203493d14be to your computer and use it in GitHub Desktop.
Save mshafae/59c6e8a6a446fa7c32b16203493d14be to your computer and use it in GitHub Desktop.
Even or odd C++ program.
// Gist https://gist.github.com/59c6e8a6a446fa7c32b16203493d14be
#include <iostream>
#include <string>
int main(int argc, char const *argv[]) {
if (argc < 2) {
std::cout << "Please provide an argument.\n";
return 1;
}
std::string argument{argv[1]};
int argument_value{-1};
try {
argument_value = stoi(argument);
} catch (std::exception const& e) {
std::cout << "What you entered (" << argument << ") is not an integer.\n";
return 1;
}
std::cout << "The number " << argument << " is ";
if (argument_value % 2 == 0) {
std::cout << "even.\n";
} else {
std::cout << "odd.\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment