# Check a number is odd or even without modulus operator

Given a number, check whether it is even or odd.

Examples :

```Input: n = 11
Output: Odd

Input: n = 10
Output: Even
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1: Using Loop.
The idea is to start with a boolean flag variable as true and switch it n times. If flag variable gets original value (which is true) back, then n is even. Else n is false.

Below is the implementation of this idea.

## C++

 `// A simple C++ program to check for ` `// even or odd ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if n is even, else odd ` `bool` `isEven(``int` `n) ` `{ ` `    ``bool` `isEven = ``true``; ` `    ``for` `(``int` `i=1;  i <= n; i++)  ` `        ``isEven = !isEven; ` `    ``return` `isEven; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 101; ` `    ``isEven(n) ? cout << ``"Even"` `: cout << ``"Odd"``; ` `    ``return` `0; ` `} `

## Java

 `// A simple Java program to  ` `// check for even or odd ` ` `  `class` `GFG { ` `     `  `    ``// Returns true if n  ` `    ``// is even, else odd ` `    ``static` `boolean` `isEven(``int` `n) ` `    ``{ ` `        ``boolean` `isEven = ``true``; ` `         `  `        ``for` `(``int` `i = ``1``; i <= n; i++)  ` `            ``isEven = !isEven; ` `             `  `        ``return` `isEven; ` `    ``} ` `     `  `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `         `  `        ``int` `n = ``101``; ` `        ``if``(isEven(n)) ` `            ``System.out.println(``"Even"``); ` `        ``else` `            ``System.out.println(``"Odd"``); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## Python3

 `# A simple Python program to  ` `# check for even or odd ` ` `  `# Returns true if n is even, ` `# else odd ` `def` `isEven(n): ` `    ``isEven ``=` `True``; ` `    ``for` `i ``in` `range``(``1``, n``+``1``): ` `        ``if` `isEven ``=``=` `True``: ` `            ``isEven ``=` `False``; ` `        ``else``: ` `            ``isEven ``=` `True``; ` ` `  `    ``return` `isEven; ` ` `  `# Driver code     ` `n ``=` `101``; ` `if` `isEven(n) ``=``=` `True``: ` `    ``print` `(``"Even"``); ` `else``: ` `    ``print` `(``"Odd"``); ` `     `  `# This code is contributed by  ` `# Manish Shaw (manishshaw1) `

## C#

 `// A simple C# program to check for ` `// even or odd ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Returns true if n is even, ` `    ``// else odd ` `    ``static` `bool` `isEven(``int` `n) ` `    ``{ ` `        ``bool` `isEven = ``true``; ` `         `  `        ``for` `(``int` `i = 1; i <= n; i++)  ` `            ``isEven = !isEven; ` `             `  `        ``return` `isEven; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 101; ` `        ``if``(isEven(n)) ` `            ``Console.Write(``"Even"``); ` `        ``else` `            ``Console.Write(``"Odd"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output:

```Odd
```

Method 2: By multiply and divide by 2. Divide the number by 2 and multiply by 2 if the result is same as input then it is an even number else it is an odd number.

## C++

 `// A simple C++ program to check for ` `// even or odd ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if n is even, else odd ` `bool` `isEven(``int` `n) ` `{ ` `    ``// Return true if n/2 does not result ` `    ``// in a float value. ` `    ``return` `((n / 2) * 2 == n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 101; ` `    ``isEven(n) ? cout << ``"Even"` `: cout << ``"Odd"``; ` `    ``return` `0; ` `} `

## Java

 `// A simple Java program ` `// to check for even or odd ` ` `  `class` `GFG { ` `     `  `// Returns true if n  ` `// is even, else odd ` `static` `boolean` `isEven(``int` `n) ` `{ ` `     `  `    ``// Return true if  ` `    ``// n/2 does not result ` `    ``// in a float value. ` `    ``return` `((n / ``2``) * ``2` `== n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``101``; ` `    ``if``(isEven(n) != ``false``) ` `        ``System.out.print( ``"Even"` `); ` `    ``else` `        ``System.out.print( ``"Odd"` `); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Smitha Dinesh Semwal. `

## Python 3

 `# A simple Python 3 program ` `# to check for even or odd ` ` `  `# Returns true if n  ` `# is even, else odd ` `def` `isEven(n): ` ` `  `     `  `    ``# Return true if  ` `    ``# n/2 does not result ` `    ``# in a float value. ` `    ``return` `(``int``(n ``/` `2``) ``*` `2` `=``=` `n) ` ` `  ` `  `# Driver code ` `n ``=` `101` `if``(isEven(n) !``=` `False``): ` `    ``print``(``"Even"``) ` `else``: ` `    ``print``(``"Odd"``) ` ` `  `# This code is contributed by  ` `# Smitha Dinesh Semwal. `

## C#

 `// A simple C# program ` `// to check for even or odd ` `using` `System; ` `class` `GFG { ` `     `  `// Returns true if n  ` `// is even, else odd ` `static` `bool` `isEven(``int` `n) ` `{ ` `     `  `    ``// Return true if  ` `    ``// n/2 does not result ` `    ``// in a float value. ` `    ``return` `((n / 2) * 2 == n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 101; ` `    ``if``(isEven(n) != ``false``) ` `        ``Console.Write(``"Even"``); ` `    ``else` `        ``Console.Write(``"Odd"``); ` `} ` `} ` ` `  `// This code is contributed by  ` `// Smitha Dinesh Semwal. `

## PHP

 ` `

Output :

```Odd
```

Method 3: Using Bitwise operator &.
A better solution is to use bitwise operators. We need to check whether last bit is 1 or not. If last bit is 1 then the number is odd, otherwise always even.

Explanation:

``` input : 5    // odd
00000101
& 00000001
--------------
00000001
--------------

input : 8     //even
00001000
& 00000001
--------------
00000000
--------------
```

Below is the implementation of the idea.

## C++

 `// A simple C++ program to check for ` `// even or odd ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true if n is even, else odd ` `bool` `isEven(``int` `n) ` `{ ` `    ``// n&1 is 1, then odd, else even ` `    ``return` `(!(n & 1)); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 101; ` `    ``isEven(n) ? cout << ``"Even"` `: cout << ``"Odd"``; ` `    ``return` `0; ` `} `

## Java

 `// A simple Java program to check for ` `// even or odd ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `public` `class` `GFG {  ` `      `  `    ``// Returns 0 if n ` `    ``// is even, else odd ` `    ``static` `int` `isEven(``int` `n) ` `    ``{ ` `          `  `        ``// n&1 is 1, then  ` `        ``// odd, else even ` `        ``return` `(n & ``1``); ` `    ``} ` `      `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``101``; ` `        ``if``(isEven(n)==``0``) ` `            ``System.out.print(``"Even"``); ` `        ``else` `            ``System.out.print(``"Odd"``); ` `    ``} ` `} ` `  `  `// This code is contributed ` `// by Manish Shaw (manishshaw1) `

## Python3

 `# A simple Python program to  ` `# check for even or odd ` `# Returns 0 if n ` `# is even, else odd ` `def` `isEven(n) : ` `      `  `    ``# n&1 is 1, then  ` `    ``# odd, else even ` `    ``return` `(n & ``1``); ` `  `  `# Driver code ` `n ``=` `101``; ` `if``(isEven(n) ``=``=` `0``) : ` `    ``print` `(``"Even"``); ` `else` `: ` `    ``print` `(``"Odd"``); ` `      `  `# This code is contributed ` `# by Manish Shaw (manishshaw1) `

## C#

 `// A simple C# program to check for ` `// even or odd ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG {  ` `     `  `    ``// Returns 0 if n ` `    ``// is even, else odd ` `    ``static` `int` `isEven(``int` `n) ` `    ``{ ` `         `  `        ``// n&1 is 1, then  ` `        ``// odd, else even ` `        ``return` `(n & 1); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 101; ` `        ``if``(isEven(n)==0) ` `            ``Console.Write(``"Even"``); ` `        ``else` `            ``Console.Write(``"Odd"``); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by Manish Shaw (manishshaw1) `

## PHP

 ` `

Output :

```Odd
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.