home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch2 / if3.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  42 lines

  1. /*                           if3.c
  2.  *   Synopsis  - The program prompts for and accepts input of a
  3.  *               decimal integer.  It then tests to see if that
  4.  *               integer is even or odd.  The results of the
  5.  *               test are displayed.
  6.  *   Objective - Illustrates an if-else statement and the use   
  7.  *               of a function call in a control expression.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Function Prototypes */
  14. int odd(int intvar );
  15.  
  16. /*   PRECONDITION:  intvar can be any integer
  17.  *
  18.  *   POSTCONDITION: The value returned is 1 if the parameter is odd
  19.  *                  and 0 if the parameter is even
  20.  */
  21.  
  22. int main(void)
  23. {
  24.      int intvar;
  25.  
  26.      printf( "Enter a decimal integer: " );
  27.      scanf( "%d", &intvar );
  28.  
  29.      if ( odd( intvar ) )                     /* Note 1 */
  30.           printf( "%d is odd.\n", intvar );
  31.      else                                     /* Note 2 */
  32.           printf( "%d is even.\n", intvar );
  33.      return 0;
  34. }
  35.  
  36. /*******************************odd()***************************/
  37.  
  38. int odd( int intvar )
  39. {
  40.      return( intvar % 2 );                    /* Note 3 */
  41. }
  42.