Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short one line solution for Tutorial '06' 'hw1' #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions 18-Programming-4kids/06_homework_01_answer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ int main() {
bool is_even1 = (num % 2 == 0);

// is even using /2
double by2 = (double) num / 2.0;// this is either X.0 or X.5 (try 10, 11)
by2 = by2 - (int) by2;// Remove X. This is now either 0 (for even) or 0.5 (for odd)
bool is_even2 = by2 == 0;
// one line solution:
// **Hint** You don't need to cast 'num' to from 'int' to 'double' like "(double)num",
// by default if you divide it as an integer / 2.0 (which is double)
// C++ will convert num to 'double' and return 'double'
bool is_even_2 = (num / 2.0 - num / 2) == 0;

// multiple lines solution:
//double by2 = (double) num / 2.0;// this is either X.0 or X.5 (try 10, 11)
//by2 = by2 - (int) by2;// Remove X. This is now either 0 (for even) or 0.5 (for odd)
//bool is_even2 = by2 == 0;

// is even using %10
int last_digit = num % 10; // even last digit is 0, 2, 4, 6, 8
Expand Down