Finding whether a no. is Even or Odd without using Conditional Statement

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");
    }
  }
}


Related Posts

  • Share/Bookmark
You can leave a response, or trackback from your own site.

12 Responses to “Finding whether a no. is Even or Odd without using Conditional Statement”

  1. How often do you write your blogs? I enjoy them a lot

  2. shabab khan says:

    is there any simple code then the above code.

    • Saurabh Saurabh says:

      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;
      }

  3. bincy says:

    why 5 is used in the second index of 2d array?

    • Saurabh Saurabh says:

      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

  4. lala says:

    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.

    • Saurabh Saurabh says:

      you can use a switch statement for this.
      switch(choice)
      { case 1:
      multiply();
      break;
      case 2:
      divide();
      break;
      default:
      printf(“Wrong choice entered”);
      }

  5. anant says:

    why dont we write anything in 1st index of array??

  6. shanu says:

    write a c++ program odd or even without using modular operator

  7. Ethan says:

    No conditions, no mods, accounts for negative numbers:

    bool isOdd(int n)
    {
    return (n & 1);
    }

  8. Toodle says:

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

Leave a Reply

| Shop Free Cellular Phones at Bestincellphones.com. | Thanks to Best CD Rates, iCellPhoneDeals.com Offers Best Cell Phone Deals. and Incinerador De Grasa