Writing a program to find whether a given number is even or odd without using any conditional statement or loop can be quite tricky. Nevertheless the program is simple and can be done in at least 2 ways.
In the first method, a 2-D array is used to store the strings “even” and “odd” in the zeroth and first row and then modulus operator is used to access either of the strings according to the number entered. This logic can be implemented in C as well as in Java. Below is the solution using this logic in C
#include "stdio.h"
main()
{ char a[][5]= {"Even","Odd"};
int n;
printf("Enter any no.: ");
scanf("%d",&n);
printf("%s",a[n%2]);
return 0;
}
In the second method, a try catch block has been used to find whether the given number is even or odd. Here if the given no. is even, an exception occurs and the control goes into the catch block, otherwise the println statement of the try block is executed.
import java.io.*;
class even
{ public static void main(String args[]) throws IOException
{ int n,check;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any number : ");
n=br.read();
try
{ check=n%2;
int a=4/check;
System.out.println("Given no. is odd");
}
catch(Exception e)
{ System.out.println("Given no. is even");
}
}
}

Posted in 

How often do you write your blogs? I enjoy them a lot
is there any simple code then the above code.
Without using any conditional statement, I dont see anyother solution.
If however, you would like to use terniary operater, you can use something like :
#include
main()
{ int n;
printf(“enter any no”);
scanf(“%d”,&n);
printf(n%2==0?”even”:”odd”);
return 0;
}
do d same without using modulus operator.
and rply me via email at contact2rajat@rediffmail.com
why 5 is used in the second index of 2d array?
5 indicates the size of the column. Since the string ‘even’ will occupy 5 bytes ( 4 for even and 1 for NULL ) I’ve used 5 .
In general, we can use any number greater than 4
What if I wanna show a division and multiplication table? I don’t know how to use a conidtional statement for it. Like “chose 1 for mult table” & “chose 2 for div table” help.
you can use a switch statement for this.
switch(choice)
{ case 1:
multiply();
break;
case 2:
divide();
break;
default:
printf(“Wrong choice entered”);
}
why dont we write anything in 1st index of array??
write a c++ program odd or even without using modular operator
No conditions, no mods, accounts for negative numbers:
bool isOdd(int n)
{
return (n & 1);
}
#include
#include
#define isODD(x) (x&1)
#define isEVEN(x) (!isODD(x))
int main(int argc, char *argv[])
{
int number, arg;
for (arg = 1; arg < argc; ++arg)
{
if (sscanf(argv[arg],"%d",&number) == 1)
{
if (isODD(number))
printf("%d is ODD\n",number);
if (isEVEN(number))
printf("%d is EVEN\n",number);
}
else printf("%s is not an integer\n",argv[arg]);
}
return EXIT_SUCCESS;
}