Table of contents
1.
Introduction
2.
Approach 
2.1.
Pseudo Code
2.2.
Implementation
2.3.
C
2.4.
C++
2.5.
Java
2.6.
Python
2.7.
JS
2.8.
C#
3.
An Approach using Bitwise AND
3.1.
Pseudo Code
3.2.
Implementation
3.3.
C
3.4.
C++
3.5.
Java
3.6.
Python
3.7.
JS
3.8.
C#
4.
Frequently Asked Questions
4.1.
How do you check if a number is odd or even?
4.2.
What is the algorithm to check whether the number is odd or even?
4.3.
What is a ternary operator?
5.
Conclusion
Last Updated: Mar 5, 2025
Easy

Program to check whether the number is even or odd

Author Urwashi Priya
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will discuss how to write a program to check whether the number is even or odd.

Program to check whether the number is even or odd

We are given a number, and we need to state whether the number is even or odd.

Example 1:

Sample Input:
a=5
Sample Output:
Odd

 

Example 2:

Sample Input:
a=6
Sample Output:
Even

Approach 

The approach to write a program to check whether the number is even or odd is given below. Check whether the number, when divided by 2, yields the remainder 0. If the remainder is 0, then it’s an even number else, it’s an odd number.

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. even or odd

Pseudo Code

Algorithm
________________________________________________________________
procedure evenodd():
___________________________________________________________________
1.   Declare n, rem
2.   rem=n%2
3.   if(rem==0): even
4.   Else: odd
5.   Display even or odd
end procedure
___________________________________________________________________

 

Implementation

  • C
  • C++
  • Java
  • Python
  • JS
  • C#

C

#include <stdio.h>

int main() {
int n, rem;

printf("Enter the number : ");
scanf("%d", &n);

// checking for the remainder
rem = n % 2;

if (rem == 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, rem;

cout << "Enter the number : ";
cin >> n;
//checking for the remainder
rem = n % 2;
if (rem == 0)
cout << "even" << endl;
else
cout << "odd" << endl;

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 scanner = new Scanner(System.in);
System.out.print("Enter the number : ");
int n = scanner.nextInt();

// checking for the remainder
int rem = n % 2;

if (rem == 0)
System.out.println("even");
else
System.out.println("odd");

scanner.close();
}
}
You can also try this code with Online Java Compiler
Run Code

Python

n = int(input("Enter the number : "))

# checking for the remainder
rem = n % 2

if rem == 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("Enter the number : ", function(n) {
// checking for the remainder
let rem = n % 2;

if (rem === 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() {
Console.Write("Enter the number : ");
int n = int.Parse(Console.ReadLine());

// checking for the remainder
int rem = n % 2;

if (rem == 0)
Console.WriteLine("even");
else
Console.WriteLine("odd");
}
}

Output:

Sample Input: 
Enter the number : 5


Sample Output:
odd

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
  • C++
  • Java
  • Python
  • JS
  • C#

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?

  1. Take input number.
  2. Divide the number by 2.
  3. If remainder is 0, print even.
  4. 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.

Live masterclass