From 52d55ac871428a363721810d9736c0ffa26f02ba Mon Sep 17 00:00:00 2001 From: Mohamed Shelf <60757136+MohamedSaid7102@users.noreply.github.com> Date: Wed, 12 Oct 2022 08:14:06 +0200 Subject: [PATCH] Short one line solution for Tutorial '06' 'hw1' --- 18-Programming-4kids/06_homework_01_answer.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/18-Programming-4kids/06_homework_01_answer.cpp b/18-Programming-4kids/06_homework_01_answer.cpp index 3f1ceb5..82b4086 100644 --- a/18-Programming-4kids/06_homework_01_answer.cpp +++ b/18-Programming-4kids/06_homework_01_answer.cpp @@ -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