Skip to content
Related Articles

Related Articles

Print “Even” or “Odd” without using conditional statement
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2021

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. 


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


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


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


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);
// This code is contributed by divyesh072019.


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


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

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


int main()
    int no;
    printf("Enter a no: ");
    scanf("%d", &no);
    (no & 1 && printf("odd"))|| printf("even");
    return 0;

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :