diff --git a/algorithms/CPlusPlus/Maths/Check-Even_Odd.cpp b/algorithms/CPlusPlus/Maths/Check-Even_Odd.cpp new file mode 100644 index 00000000..6bae63f2 --- /dev/null +++ b/algorithms/CPlusPlus/Maths/Check-Even_Odd.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +//Function to check whether a number is Odd or not. +//We use a single Odd function to check for Even-Odd, If it return true then it is Odd else it is Even. +bool isOddBin(int num){ + //We compare the last bit of the number to decide for Even or Odd + //If the last bit of the num is set (i.e. 1) then number is odd and its "Binary And (&)" with the 1 will return true(1). + //Similarly in case of even num last bit is unset/off (i.e. 0) and its "Binary And (&)" with the 1 will return false(0). + return (num&1); +} + +bool isOddN(int num){ + //In this we use the Mod(%) operator to check for Even-Odd. + //If the num is Odd then (num%2) will give 1 and function will return true. + //If the num is Even then (num%2) will give 0 and function will return false. + return (num%2==1); +} + +int main() { + int num; + cin >> num; //Taking input from the user + string result; + result = isOddBin(num) ? "Odd" : "Even"; //Evaluating string based on result from isOdd function + cout << result<