Last active
December 13, 2023 23:15
-
-
Save mshafae/59c6e8a6a446fa7c32b16203493d14be to your computer and use it in GitHub Desktop.
Even or odd C++ program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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