Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Even or Odd

Easy
0/40
Average time to solve is 6m
6 upvotes

Problem statement

Write a program that uses bitwise AND operator to check if a given positive integer is even or odd.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
A single Integer, N
Output Format:
Print "Odd” if N is odd otherwise "Even".
Constraints:
1<=N<=10^4
Even or Odd
All tags
Sort by
Search icon

Interview problems

99.12 % better

int main() {

    int n;

    cin>>n;

    if(n & 1){

        cout<<"Odd";

    }

    else{

      cout<<"Even";

    }

    return 0 ;

}

 

36 views
0 replies
1 upvote

C++

#include<iostream>

using namespace std;

 

int main() {

    // Write your code here

    int n;

    cin>>n;

 

    if(n&1){

        cout<<"Odd"<<endl;

    }else{

        cout<<"Even"<<endl;

    }

    

}

313 views
0 replies
1 upvote

Interview problems

C++

#include<iostream>

using namespace std;

 

int main() {

    int num;

 

    // Get input from the user

    

    std::cin >> num;

 

    // Use bitwise AND operator to check the least significant bit

    // If the least significant bit is 0, the number is even; otherwise, it's odd

    if (num & 1) {

        std::cout << "Odd"<< std::endl;

    } else {

        std::cout << "Even" << std::endl;

    }

 

    return 0;

}

    

 

238 views
0 replies
0 upvotes
Full screen
Console