Saturday, 25 February 2017

GATE EXAM PREPARATION IN "C" - Print “Even” or “Odd” without using conditional statement

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. Your 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.
#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;
}

No comments:

Post a Comment