Programming Example
Write a program in C that determines if a year is a leap year in different approach
Write a program in C that determines if a year is a leap year in different approach
#include<stdio.h>
int main()
{
int year, rem_4,rem_100,rem_400;
printf("Enter the year to be tested:");
scanf("%d", &year);
rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;
if( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0)
printf("It is a leap year.\n");
else
printf("It is not a leap year.\n");
getch();
return 0;
}
<b>Output 1:</b>
Enter the year to be tested:2100
It is not a leap year.
<b>Output 2:</b>
Enter the year to be tested:2012
It is a leap year.
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.