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
Page Contents
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
Source Code: C Program To Determine Leap Year or Not using Logical Operators
- #include < stdio.h >
- int main()
- {
- int year;
- printf("Enter a year\n");
- scanf("%d", &year);
- if( (year%100 == 0 && year%400 == 0) ||
- (year%100 != 0 && year%4 == 0) )
- {
- printf("%d is a leap year\n", year);
- }
- else
- {
- printf("%d is not a leap year\n", year);
- }
- return 0;
- }
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