C
O
D
E
PROGRAM TO CHECK LEAP YEAR:
#include<stdio.h>
int main()
{
int year;
printf(“Enter a year: “);
scanf(“%d”, &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf(“%d is a leap year.\n”, year);
}
else
{
printf(“%d is not a leap year.\n”, year);
}
return 0;
}
OUTPUT:
Enter a year: 2004
2004 is a leap year.
DOWNLOAD CODE:
Table of Contents
E X E C U T E
E X P L A N A T I O N
EXPLAIN PROGRAM TO CHECK LEAP YEAR:
ThIs C program checks if a given year is a leap year and prints the result.
Program Overview:
This C program to check leap year serves a straightforward purpose. A leap year has an extra day (February 29) and occurs every four years, with some exceptions. The code utilizes if and else statements to evaluate the conditions that define a leap year and then provides output accordingly.
Code Explanation:
#include <stdio.h>
: This line is a preprocessor directive that includes the standard input-output library in the program. It’s necessary for input and output operations.int main()
: This is the main function of the program. It is the entry point of the program, and execution starts from here.int year;
: This line declares an integer variable namedyear
to store the user’s input, which will represent the year to be checked.printf("Enter a year: ");
: This statement uses theprintf
function to display the “Enter a year: ” message on the console, prompting the user to input a year.scanf("%d", &year);
: This line uses thescanf
function to read the integer value entered by the user and store it in theyear
variable. The%d
format specifier is used to read an integer.if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
: This is the beginning of the if statement. It checks the conditions for a leap year.(year % 4 == 0 && year % 100 != 0)
: The first part of the condition checks whether the year is divisible by 4 (i.e.,year % 4 == 0
) and not divisible by 100 (i.e.,year % 100 != 0
). This is a common rule for leap years.||
: This is a logical OR operator, which means the condition will be true if either the first part or the second part is true.(year % 400 == 0)
: The second part of the condition checks whether the year is divisible by 400. This is an exception to the first condition. If a year is divisible by 400, it is considered a leap year.
printf("%d is a leap year.\n", year);
: If the condition in theif
statement is true, this line will execute. It uses theprintf
function to print a message indicating that the input year is a leap year, along with the year itself.else {
: If the condition in theif
statement is false, execution proceeds to theelse
block.printf("%d is not a leap year.\n", year);
: In theelse
block, this line usesprintf
to display a message indicating that the input year is not a leap year, along with the year itself.return 0;
: Finally, thereturn
statement is used to exit the program. The value0
indicates a successful execution of the program. Themain
function is of typeint
, and it’s customary to return an integer value to indicate the program’s status (0 for success).
How the Code Works:
- The program starts by prompting the user to input a year.
- The user enters a year, and the program stores it in the
year
variable. - The
if
statement is used to check whether the input year is a leap year, based on the conditions defined for leap years. If the conditions are met, the program prints that it’s a leap year. Otherwise, it prints that it’s not a leap year. - The program exits with a status of 0, indicating successful execution.
Leap Year Calculation:
The code is based on the rules for determining leap years:
- A year is a leap year if it’s divisible by 4.
- However, years divisible by 100 are not leap years, unless…
- …they are divisible by 400, in which case they are considered leap years.
For example, the year 2000 is a leap year because it’s divisible by 4 and also divisible by 400. The year 1900, on the other hand, is not a leap year because it’s divisible by 4 and 100, but not by 400.
Sample Execution:
Let’s illustrate how the code works with a sample execution:
- The program prompts the user to enter a year.
- The user enters the year 2024.
- The program checks the conditions:
(2024 % 4 == 0 && 2024 % 100 != 0)
is true, and(2024 % 400 == 0)
is not needed. Therefore, the program prints “2024 is a leap year.” - The program exits with a status of 0.
Conclusion:
In summary, this C program checks if a given year is a leap year by applying the rules that define leap years. It uses the if and else statements to evaluate the conditions and provides a clear message to the user based on the outcome of the check. The program is a simple but useful tool for identifying leap years, and it demonstrates the use of conditional statements in C.
MORE PROGRAM :
- https://d-techsol.in/in-c-programming-write-a-code-of-lcm-of-two-number/
- https://d-techsol.in/write-a-program-of-addition-of-number-in-c/
- https://d-techsol.in/fibonacci-series-in-c-program-using-for-loop/
- https://d-techsol.in/easy-code-of-factorial-in-c-program/
- https://d-techsol.in/4-factorial-using-recursion-of-a-number-in-c-program/
- https://d-techsol.in/4-fibonacci-series-in-c-program-using-recursion/
BEST C PROGRAMMING BOOK :