Write a C/C++ 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 (if, else, switch, ternary operator,..etc) statement.
Method 1
Below is a tricky code can be used to print “Even” or “Odd” accordingly.
C++
#include<iostream> #include<conio.h> using namespace std; int main() { char arr[2][5] = {"Even", "Odd"}; int no; cout << "Enter a number: "; cin >> no; cout << arr[no%2]; getch(); return 0; } |
Python3
arr = ["Even", "Odd"] print ("Enter the number") no = input() print (arr[int(no) % 2]) |
PHP
<?php $arr = ["Even", "Odd"]; $input = 5; echo ($arr[$input % 2]); // This code is contributed // by Aman ojha ?> |
Method 2
Below is another tricky code can be used to print “Even” or “Odd” accordingly. Thanks to student for suggesting this method.
#include<stdio.h> 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implementing ternary operator without any conditional statement
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Conditionally assign a value without using conditional and arithmetic operators
- Programming puzzle (Assign value without any control statement)
- Can we write a print statement within if parentheses?
- Conditional or Ternary Operator (?:) in C/C++
- How will you print numbers from 1 to 100 without using loop?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Print substring of a given string without using any string function and loop in C
- Print individual digits as words without using if or switch
- Print "Hello World" in C/C++ without using any header file
- C Program to print numbers from 1 to N without using semicolon?
- How to print a semicolon(;) without using semicolon in C/C++?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- How to print a number 100 times without using loop and recursion in C?
- C program to print characters without using format specifiers
- Print a number 100 times without using loop, recursion and macro expansion in C?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print a character n times without using loop, recursion or goto in C++
- How to print N times without using loops or recursion ?

