Programming Example
One line C program to check if a given year is leap year or not
One line C program to check if a given year is leap year or no
/* Program to find that entered year is leap year or not.
Author: Atnyla Developer */
// One line C program to check if a given
// year is leap year or not
#include <stdio.h>
#include <stdbool.h>
bool checkYear(int year)
{
// Return true if year is a multiple pf 4 and
// not multiple of 100.
// OR year is multiple of 400.
return (((year%4==0) && (year%100!=0)) ||
(year%400==0));
}
int main()
{
int year;
printf("Enter a year to be check \n");
scanf("%d", &year);
checkYear(year)? printf("Leap Year \n"):
printf("Not a Leap Year \n");
return 0;
}
<b>Output 1 </b>
Enter a year to be check
2100
Not a Leap Year
Press any key to continue . . .
<b>Output 2 </b>
Enter a year to be check
2012
Leap Year
Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.