Leap Year

Concepts:
If statement and boolean expressions

Text:
Given a year (an integer number), say if it is a leap year. A leap year is a year:

Examples: 1996, 2000 and 2100 are leap years; instead 2015, 1900 and 2100 are not leap years.

Solution:

leap year.c
/*
Given a year (an integer number), say if it is a leap year.
A leap year is divisible by 4, but not by 100.
An exception are the years divisible by 400, which are leap years.
*/
 
#include <stdio.h>
 
 
int main(void){
  int leap=0;
  int year;
 
  printf("Insert an year: ");
  scanf("%d", &year);
 
  /* First solution: base on a support varible leap */
  if ( year%4==0 )   /* divisible by 4 */
    leap=1;
 
  if ( year%100==0 ) /* but not by 100 */
    leap=0;
 
  if ( year%400==0 ) /* but year divisible by 400 are leap years */
    leap=1;
 
  if ( leap==1 ){
    printf("%d is a leap year\n", year);
  }else{
    printf("%d is not a leap year\n", year);
  }
 
  /* Second solution: using a boolean expression */
  if ( (year%4==0 && year%100!=0) || year%400==0 ){
    printf("%d is a leap year\n", year);
  }else{
    printf("%d is not a leap year\n", year);
  }
 
  return 0;
}