cs:c_language:leap_year
Return to Home page
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?rev=1551188114&do=diff
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:leap_year [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 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: | ||
+ | * 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: | ||
+ | |||
+ | <file C 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 < | ||
+ | |||
+ | |||
+ | int main(void){ | ||
+ | int leap=0; | ||
+ | int year; | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | |||
+ | /* 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(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | /* Second solution: using a boolean expression */ | ||
+ | if ( (year%4==0 && year%100!=0) || year%400==0 ){ | ||
+ | printf(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | 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
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?rev=1551188114&do=diff