C Program To Determine Leap Year or Not using Logical Operators


Any year is entered through the keyboard, write a C program to determine whether the year is a leap year or not. Use the logical operators && and ||.

Related Read:
if else statement in C
Relational Operators In C
Logical Operators In C

Other Leap Year C Programs

C Program To Check Leap Year
C Program To Check Leap Year Using Ternary Operator

Leap Year Logic

1. If a year is a century year(year ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Century year is determined by modulo division of the entered year by 100. Example: year%100 == 0. If its century year, it must also be perfectly divisible by 400. i.e., year%400 == 0
(year%100 == 0 && year%400 == 0)

If the year is not century year, then it must be perfectly divisible by 4, for the year to be a leap year.
(year%100 != 0 && year%4 == 0)

Expected Output for the Input

User Input:
Enter a year
2024

Output:
2024 is a leap year

Video Tutorial: C Program To Determine Leap Year or Not using Logical Operators



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

Source Code: C Program To Determine Leap Year or Not using Logical Operators

  1. #include < stdio.h >  
  2.   
  3. int main()  
  4. {  
  5.     int year;  
  6.   
  7.     printf("Enter a year\n");  
  8.     scanf("%d", &year);  
  9.   
  10.     if( (year%100 == 0 && year%400 == 0) ||  
  11.         (year%100 != 0 && year%4   == 0)   )  
  12.     {  
  13.         printf("%d is a leap year\n", year);  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("%d is not a leap year\n", year);  
  18.     }  
  19.   
  20.     return 0;  
  21. }  

Output 1:
Enter a year
2020
2020 is a leap year

Output 2:
Enter a year
2021
2021 is not a leap year

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 *