0 Replies - 1506 Views - Last Post: 24 July 2011 - 06:28 PM Rate Topic: -----

#1 stackoverflow   User is offline

  • D.I.C Addict
  • member icon

Reputation: 166
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Functions to evaluate even or odd numbers

Posted 24 July 2011 - 06:28 PM

Description: The functions assume the input is correct. I have also included defines for true and false to make the code more readable.

A sample usable is given. These are two functions you can use to evaluate if an integer is even or odd.
#include <stdio.h>

#define TRUE  1
#define FALSE 0

/* 
* isEven - checks if an integer is even (or odd)
* @value: the value you wish to check
* @return: returns 1/true on even and 0/false on odd
*/
int isEven(int value)
{
  return !(value % 2) ? TRUE : FALSE; 
}

/* 
* isOdd - checks if an integer is odd (or even)
* @value: the value you wish to check
* @return: returns 1/true on odd and 0/false on even
*/
int isOdd(int value)
{
  return !(value % 2) ? FALSE : TRUE; 
}

/* sample usage below */

int main(void)
{
  int i = 14, z = 667; 

  if (isEven(i)){
    printf("%d is even!n", i);
  }

  if (isEven(z)){
    printf("%d is even!n", z);
  } else {
    printf("%d is odd!n", z);
  }

  return 0;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1