cs:c_language:leap_year

Return to Home page

# Leap Year

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

**You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/leap_year?do=**

**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:

- divisible by 4
- but not by 100
- with the exception of the years divisible by 400, which are leap years

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

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/leap_year.txt · Last modified: 2019/02/26 14:35 (external edit)