Use nested if statements to see whether you enter an odd or an even number. - C Statement

C examples for Statement:if

Description

Use nested if statements to see whether you enter an odd or an even number.

Demo Code

#include <stdio.h>
#include <limits.h>               // For LONG_MAX

int main(void){
  long test = 0L;                 // Stores the integer to be checked

  printf("Enter an integer less than %ld:", LONG_MAX);
  scanf(" %ld", &test);

  // Test for odd or even by checking the remainder after dividing by 2
  if(test % 2L == 0L){
    printf("The number %ld is even", test);

    if((test/2L) % 2L == 0L){
      printf("\nHalf of %ld is also even", test);
      printf("\nThat's interesting isn't it?\n");
    }//from  w  w w  . j ava2 s.co m
  }
  else
    printf("The number %ld is odd\n", test);
  return 0;
}

Result


Related Tutorials