Explanation
#include <stdio.h>
is a preprocessor directive that includes the standard
input/output library in
the program. This library contains functions such as printf()
and scanf()
that are used to
display output and take input from the user, respectively.
int main()
is the main function of the program. It is the entry point of the program,
where the
execution of the program starts.
In the main function, an integer variable d
is declared and a prompt is displayed to
the user asking
them to enter a year. Then, the value entered by the user is stored in the d
variable
using the
scanf()
function.
Next, the program checks if the entered year is a leap year or not. A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not divisible by 400. If the year is a leap year, a message is displayed stating that it is a leap year. Otherwise, the message states that it is not a leap year.
Finally, the program returns 0, indicating that the program executed successfully.
Some additional functions present in the stdio.h
header file are:
fprintf()
- This function is used to print output to a specific file.fscanf()
- This function is used to read input from a specific file.
The output of the program will look like this:
Program to check if an entered year is leap or not with if else statement.
Enter an year - (Example: 2100) - and press enter: 2000
The entered year is a leap year.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
int d;
printf("\n\n Program to check if an entered year is leap or not with if else statement. \n\n");
printf("Enter an year - (Example: 2100) - and press enter: ");
scanf("%d", &d);
printf("\n The entered year is");
if (d % 4 == 0 || d % 400 == 0)
{
printf(" a leap year.\n");
}
else
{
printf(" not a leap year.\n");
}
return 0;
}
Output
Program to check if an entered year is leap or not with if else statement.
Enter an year - (Example: 2100) - and press enter: 2003
The entered year is not a leap year.