C program to check whether a year is leap year or not - Source code

int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0) // check divisibility by 4
{
if( year%100 == 0) // check if year is a century year
{
if ( year%400 == 0) // check divisibility by 400
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}

Program Output

Case 1:
Enter a year: 2000
2000 is a leap year.
Case 2:
Enter a year: 2103
2103 is not a leap year.

Program Explanation

1. As mentioned above, if a year is divisible by 4 but not by 100, then it is a leap year. Also, if a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.

2. First an if statement is used to check whether the entered year is divisible by 4. If true, the year is checked for the second condition, otherwise it is printed as non-leap year.(because if a number is not divisible by 4, it will not be divisible by 400).

3. In first nested if statement, the year is checked for divisibility by 100(to check for century year). If it is divisible by 100, it is again checked for divisibility by 400. If true it is a leap year else non leap year.

C program to check whether a year is leap year or not - Source codeProgram OutputProgram Explanation1. As mentioned above, if a year is divisible by 4 but not by 100, then it is a leap year. Also, if a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.

2. First an if statement is used to check whether the entered year is divisible by 4. If true, the year is checked for the second condition, otherwise it is printed as non-leap year.(because if a number is not divisible by 4, it will not be divisible by 400).

3. In first nested if statement, the year is checked for divisibility by 100(to check for century year). If it is divisible by 100, it is again checked for divisibility by 400. If true it is a leap year else non leap year.

need an explanation for this answer? contact us directly to get an explanation for this answer