
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Print “Even” or “Odd” without using conditional statement
In this section we will see how to check whether a number is odd or even without using any kind of conditional statements like (<, <=, !=, >, >=, ==).
We can easily check the odd or even by using the conditional statements. We can divide the number by 2, then check whether the remainder is 0 or not. if 0, then it is even. Otherwise we can perform AND operation with the number and 1. If the answer is 0, then it is even, otherwise odd.
Here no conditional statements can be used. We will see two different methods to check the odd or even.
Method 1
Here we will create an array of strings. The index 0 position will hold “Even”, and index 1 position will hold “Odd”. We can send the remainder after dividing the number by 2 as index to get the result directly.
Example Code
#include <iostream> using namespace std; main() { int n; string arr[2] = {"Even", "Odd"}; cout << "Enter a number: "; //take the number from the user cin >> n; cout << "The number is: " << arr[n%2]; //get the remainder to choose the string }
Output 1
Enter a number: 40 The number is: Even
Output 2
Enter a number: 89 The number is: Odd
Method 2
This is the second method. In this method we will use some tricks. Here logical and bitwise operators are used. At first we are performing AND operation with the number and 1. Then using logical and to print the odd or even. When the result of bitwise AND is 1, then only the logical AND operation will return the odd result, otherwise it will return even.
Example Code
#include <iostream> using namespace std; main() { int n; string arr[2] = {"Even", "Odd"}; cout << "Enter a number: "; //take the number from the user cin >> n; (n & 1 && cout << "odd")|| cout << "even"; //n & 1 will be 1 when 1 is present at LSb, so it is odd. }
Output 1
Enter a number: 40 even
Output 2
Enter a number: 89 odd
- Related Articles
- C Program to print “Even” or “Odd” without using Conditional statement
- C++ Program to Check Whether Number is Even or Odd
- Java program to Print Odd and Even Number from an Array
- C Program to Check if count of divisors is even or odd?
- C program to print characters without using format specifiers
- Maximum of four numbers without using conditional or bitwise operator in C++
- How to print a name multiple times without loop statement using C language?
- Java program to print odd and even number from a Java array.
- C Program for Print individual digits as words without using if or switch.
- Program to print root to leaf paths without using recursion using C++
- C Program to print “Hello World!” without using a semicolon
- C program to print number series without using any loop
- Java program to find whether given number is even or odd
- Java Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Even or Odd
