Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Print “Even” or “Odd” without using conditional statement

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Write a program that accepts a number from the user and prints “Even” if the entered number is even and prints “Odd” if the number is odd. You are not allowed to use any comparison (==, <,>,…etc) or conditional statements (if, else, switch, ternary operator,. Etc).

Method 1 
Below is a tricky code can be used to print “Even” or “Odd” accordingly. 

C++




#include <iostream>
 
using namespace std;
 
int main()
{
    char arr[2][5] = { "Even", "Odd" };
    int no;
    cout << "Enter a number: ";
    cin >> no;
    cout << arr[no % 2];
    getchar();
    return 0;
}


Java




import java.util.Scanner;
class GFG
{
    public static void main(String[] args)
    {
         
        String[] arr = {"Even", "Odd"};
         
        Scanner s = new Scanner(System.in);
         
        System.out.print("Enter the number: ");
        int no = s.nextInt();
 
        System.out.println(arr[no%2]);
    }
}
 
// This code is contributed by divyeshrabadiya07.


Python3




arr = ["Even", "Odd"]
print ("Enter the number")
no = int(input())
print (arr[no % 2])


C#




using System;
class GFG {
  static void Main() {
    string[] arr = {"Even", "Odd"};
      
    Console.Write("Enter the number: ");
     
    string val;
    val = Console.ReadLine();
    int no = Convert.ToInt32(val);
 
    Console.WriteLine(arr[no%2]);
  }
}
 
// This code is contributed by divyesh072019.


PHP




<?php
$arr = ["Even", "Odd"];
$input = 5;
echo ($arr[$input % 2]);
 
// This code is contributed
// by Aman ojha
?>


Javascript




<script>
 
    let arr = ["Even", "Odd"];
    let no = prompt("Enter a number: ");
     
    document.write(arr[no % 2]);
     
   // This code is contributed by suresh07
    
</script>


Time Complexity: O(1)
Auxiliary Space: O(1)

Method 2 
Below is another tricky code can be used to print “Even” or “Odd” accordingly. Thanks to student for suggesting this method.

C++




#include <iostream>
using namespace std;
 
int main()
{
    int no = 8;
   
    (no & 1 && cout << "odd" )|| cout << "even";
   
    return 0;
}
 
// This code is contributed by sarajadhav12052009


C




#include<stdio.h>
int main()
{
    int no = 8;
    (no & 1 && printf("odd"))|| printf("even");
    return 0;
}


Java




import java.util.*;
 
class GFG
{
public
    static void main(String[] args)
    {
        int no = 8;
 
        if ((no & 1) != 0)
        {
            System.out.println("odd");
        }
        else
        {
            System.out.println("even");
        }
    }
}


Python3




no = 8
if no & 1:
    print("odd")
else:
    print("even")


C#




using System;
 
class GFG
{
    static void Main(string[] args)
    {
        int no = 8;
 
        if ((no & 1) != 0)
        {
            Console.WriteLine("odd");
        }
        else
        {
            Console.WriteLine("even");
        }
    }
}


Javascript




let no = 8;
 
(no & 1 && console.log("odd")) || console.log("even");


Output

even

Time Complexity: O(1)
Auxiliary Space: O(1)

Method 3
This can also be done using a concept known as Branchless Programming. Essentially, make use of the fact that a true statement in Python (other some other languages) evaluates to 1 and a false statement evaluates to false.
 

C++




#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
 
    if (n % 2 == 0) {
        cout << "Even" << endl;
    }
    else {
        cout << "Odd" << endl;
    }
 
    return 0;
}
 
// This code is contributed by surajrasr7277.


Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
          int n = 8;
        if (n % 2 == 0) {
            System.out.println("Even");
        }
        else {
            System.out.println("Odd");
        }
    }
}


Python3




# code
n = 8
print("Even" * (n % 2 == 0), "Odd" * (n % 2 != 0))


Javascript




let n = 8;
 
if (n%2==0)
    document.write("Even");
else
    document.write("Odd");
  


C#




// This code checks whether a given number is even or odd.
using System;
 
class MainClass {
    public static void Main()
    {
        Console.Write("Enter a number: ");
        int n = Convert.ToInt32(Console.ReadLine());
        if (n % 2 == 0) {
            Console.WriteLine("Even");
        }
        else {
            Console.WriteLine("Odd");
        }
    }
}


Output

Enter a number: Even 

Time Complexity: O(1)
Auxiliary Space: O(1)

Please write comments if you find the above code incorrect, or find better ways to solve the same problem


My Personal Notes arrow_drop_up
Last Updated : 30 Mar, 2023
Like Article
Save Article
Similar Reads