An Approach using Bitwise AND
The approach to write a program to check whether the number is even or odd using the ternary operator is given below. A number is said to be an odd number if it contains 1 as its rightmost bit in Bitwise representation. A number is said to be an even number if it has 0 as its rightmost bit in bitwise representation.
Time Complexity for writing a program to check whether the number is even or odd = O(1).
It takes constant time.
Till now, I assume you must have got the basic idea of what has been asked in the problem statement. So, I strongly recommend you first give it a try.
program to check whether the number is even or odd
Pseudo Code
Algorithm
___________________________________________________________________
procedure evenodd():
___________________________________________________________________
1. Declare n
2. If (n&1 == 0): even
3. Else odd.
end procedure
___________________________________________________________________
Implementation
C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if ((n & 1) == 0)
printf("even\n");
else
printf("odd\n");
return 0;
}

You can also try this code with Online C Compiler
Run Code
C++
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
if((n & 1) == 0)
cout<<"even";
else
cout<<"odd";
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if ((n & 1) == 0)
System.out.println("even");
else
System.out.println("odd");
sc.close();
}
}

You can also try this code with Online Java Compiler
Run Code
Python
n = int(input())
if (n & 1) == 0:
print("even")
else:
print("odd")

You can also try this code with Online Python Compiler
Run Code
JS
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("", function(n) {
if ((n & 1) == 0)
console.log("even");
else
console.log("odd");
rl.close();
});

You can also try this code with Online Javascript Compiler
Run Code
C#
using System;
class Program {
static void Main() {
int n = int.Parse(Console.ReadLine());
if ((n & 1) == 0)
Console.WriteLine("even");
else
Console.WriteLine("odd");
}
}
Output:
Sample Input:
Enter the number : 5
Sample Output:
odd
Frequently Asked Questions
How do you check if a number is odd or even?
A number is even if divisible by 2 without remainder (n % 2 == 0), otherwise, it is odd.
What is the algorithm to check whether the number is odd or even?
- Take input number.
- Divide the number by 2.
- If remainder is 0, print even.
- Else, print odd.
What is a ternary operator?
It consists of three operands, starting with a condition, followed by a question mark(?) and then a colon(:).
Syntax: condition ? expression1 : expression2;
Conclusion
This article taught us how to write a program to swap two numbers.
We discussed its implementation using a temporary variable and without using a temporary variable.
We hope you can take away critical techniques like analyzing problems by walking over the execution of the steps and finding out the pattern followed in most array problems.