Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.
Related Read:
C Program To Check Leap Year
Video Tutorial: C Program To Check Leap Year or Not using Function
Source Code: C Program To Check Leap Year or Not using Function
- #include<stdio.h>
- #include<stdbool.h>
- bool leap(int); // function prototype
- int main()
- {
- int year;
- printf("Enter a year to find leap year or not\n");
- scanf("%d", &year);
- //function call leap(year);
- if( leap(year) )
- {
- printf("%d is leap year\n", year);
- }
- else
- {
- printf("%d is not leap year\n", year);
- }
- return 0;
- }
- //function definition
- bool leap(int year)
- {
- if(year % 100 == 0)
- {
- if(year % 400 == 0)
- return true;
- else
- return false;
- }
- else
- {
- if(year % 4 == 0)
- return true;
- else
- return false;
- }
- }
Output 1
Enter a year to find leap year or not
2020
2020 is leap year
Output 2
Enter a year to find leap year or not
2021
2021 is not leap year
Logic To Find Leap Year or Not: Using Function
If user entered year is a century year(year ending with 00) and the year is perfectly divisible by 400, then it’s a leap year.
If the user entered year is not a century year and the year is perfectly divisible by 4, then its a leap year orelse it’s not a leap year.
In our function, we return true if the user entered year is leap year and return false if the user entered year is not leap year.
Note: User defined function leap has a return type of bool i.e., it returns either true or false. For our C program to support bool type we are including stdbool.h header file.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert