Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Approach 
2.1.
Pseudo Code
2.2.
Implementation in C++
2.2.1.
Complexity Analysis
3.
Approach using Bitwise AND
3.1.
Pseudo Code
3.2.
Implementation in C++
3.2.1.
Complexity Analysis
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Program to check whether the number is even or odd

Author Urwashi Priya
1 upvote

Introduction

This blog will discuss how to write a 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 in 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

 

Output:

Sample Input: 
Enter the number : 5


Sample Output:
odd

 

Complexity Analysis

Time Complexity: O(1), Since no extra operation, constant time is taken.

Space complexity: O(1), no extra variable used. 

Read More - Time Complexity of Sorting Algorithms

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 in 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

 

Output:

Sample Input: 
Enter the number : 5

Sample Output:
odd
You can also try this code with Online C++ Compiler
Run Code

 

Complexity Analysis

Time Complexity: O(1), Since no extra operation, constant time is taken.

Space complexity: O(1), no extra variable used. 

Try and compile with online c++ compiler.
Also readDecimal to Binary c++

FAQs

  1. Is there any other method to check whether the number is even or odd besides the one discussed in this blog?
    Yes, we can check if the number is even or odd using the ternary operator. Check whether the number, when divided by 2, yields the remainder 0. This check is being done using the ternary operator. If the condition is true, then it’s an even number else, it’s an odd number. 
     
  2. 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;
     
  3. What is time complexity? What is space complexity?
    Time complexity measures the time taken by a computer to run an algorithm. It is of three types: best time complexity, average time complexity and worst time complexity. Space complexity defines the amount of extra space used in an algorithm.    

Key Takeaways

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 could take away critical techniques like analysing problems by walking over the execution of the steps and finding out the pattern followed in most array problems.

Now, we recommend you practice problem sets based on arrays to master your fundamentals. You can get a wide range of questions similar to this on Coding Ninjas Studio

It's not the end. Must solve the problem of similar types.

Read about Bitwise Operators in C and Strong number in c here.

Happy Coding.

Live masterclass