C Program To Check Leap Year or Not using Function


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



YouTube Link: https://www.youtube.com/watch?v=bFYhQJHXWlU [Watch the Video In Full Screen.]

Source Code: C Program To Check Leap Year or Not using Function

  1. #include<stdio.h>  
  2. #include<stdbool.h>  
  3.   
  4. bool leap(int); // function prototype  
  5.   
  6. int main()  
  7. {  
  8.     int year;  
  9.   
  10.     printf("Enter a year to find leap year or not\n");  
  11.     scanf("%d", &year);  
  12.   
  13.     //function call leap(year);  
  14.     if( leap(year) )  
  15.     {  
  16.         printf("%d is leap year\n", year);  
  17.     }  
  18.     else  
  19.     {  
  20.         printf("%d is not leap year\n", year);  
  21.     }  
  22.   
  23.     return 0;  
  24. }  
  25.   
  26. //function definition  
  27. bool leap(int year)  
  28. {  
  29.     if(year % 100 == 0)  
  30.     {  
  31.         if(year % 400 == 0)  
  32.             return true;  
  33.         else  
  34.             return false;  
  35.     }  
  36.     else  
  37.     {  
  38.         if(year % 4 == 0)  
  39.             return true;  
  40.         else  
  41.             return false;  
  42.     }  
  43. }  

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

Leave a Reply

Your email address will not be published. Required fields are marked *