Determining if a year qualifies as a leap year is a frequent activity in C programming when handling date and time computations. A leap year consists of 366 days instead of the usual 365. This occurs because the Earth requires slightly more than 365 days to orbit the sun. To maintain the calendar’s precision, we insert an additional day into February every four years, resulting in it having 29 days. In this article, we will explore what defines a leap year, the guidelines for identifying a leap year, pseudocode for leap year determination in C, and identifying a leap year in C using if-else, while loop, switch statement, function, and ternary operator.
A leap year is characterized by having one additional day compared to a standard year, which contains 365 days. A leap year totals 366 days, with the extra day added to February, occurring every four years.
Per the Gregorian calendar, a solar year is roughly 365.25 days, which is recognized as a standard year. The additional 0.25 days each year accumulate over four years to equal one complete day, thereby extending the year to 366 days.
Guidelines for Identifying a Leap Year
To determine whether a year is a leap year, you need to adhere to these guidelines.
A year qualifies as a leap year if:
It is divisible by 4 but not divisible by 100.
It is divisible by 400 but not divisible by 100.
For clarity, refer to the following table:
Condition
Outcome
Not divisible by 4
Not a leap year
Divisible by 4, not by 100
Leap year
Divisible by 100, not by 400
Not a leap year
Divisible by 400
Leap year
Visual Workflow Diagram of a Leap Year in C
The workflow diagram effectively illustrates the logic for determining a leap year in a C program.
Pseudocode for a Leap Year in C
START
INPUT year
IF (year is NOT divisible by 4)
PRINT "Not a leap year"
ELSE IF (year is NOT divisible by 100)
PRINT "Leap year"
ELSE IF (year is divisible by 400)
PRINT "Leap year"
ELSE
PRINT "Not a leap year"
END
Overview:
If the year is not divisible by 4, then it is considered a common year.
If the year is divisible by 4 but not by 100, then that year is classified as a leap year.
If the year is divisible by 100, further verification is needed:
If the year is also divisible by 400, then it is indeed a leap year.
Otherwise, the year is not a leap year.
C Program to Validate Leap Year Using if-else Statement
Cpp
Code Copied!
var isMobile = window.innerWidth “);
editor68246.setValue(decodedContent); // Set the default text
editor68246.clearSelection();
editor68246.setOptions({
maxLines: Infinity
});
function decodeHTML68246(input) {
var doc = new DOMParser().parseFromString(input, “text/html”);
return doc.documentElement.textContent;
}
“““javascript
// Function to duplicate code to clipboard
function duplicateCodeToClipboard68246() {
const code = editor68246.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert(“Code duplicated to clipboard!”);
function dismissOutput68246() {
var code = editor68246.getSession().getValue();
jQuery(".maineditor68246 .code-editor-output").hide();
}
// Assign event listeners to the buttons
document.getElementById("copyBtn68246").addEventListener("click", duplicateCodeToClipboard68246);
document.getElementById("runBtn68246").addEventListener("click", executeCode68246);
document.getElementById("closeoutputBtn68246").addEventListener("click", dismissOutput68246);
Output:
The code illustrates how the year 2024 is input in a C program to determine if it is a leap year or not by utilizing the if-else statement along with the divisibility conditions, and then outputs the result “2024 is a leap year” to the console.
C Program to Determine Leap Year via Ternary Operator
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor50464.setValue(decodedContent); // Initialize the default text
editor50464.clearSelection();
editor50464.setOptions({
maxLines: Infinity
});
function decodeHTML50464(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code to clipboard
function duplicateCodeToClipboard50464() {
const code = editor50464.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code duplicated to clipboard!");
function dismissOutput50464() {
var code = editor50464.getSession().getValue();
jQuery(".maineditor50464 .code-editor-output").hide();
}
// Assign event listeners to the buttons
document.getElementById("copyBtn50464").addEventListener("click", duplicateCodeToClipboard50464);
document.getElementById("runBtn50464").addEventListener("click", executeCode50464);
document.getElementById("closeoutputBtn50464").addEventListener("click", dismissOutput50464);
Output:
The code demonstrates how the year 2025 is given as input in a C program to verify if it is a leap year or not, by using the ternary operator and the divisibility criteria, and subsequently prints the result “2025 is not a leap year” to the console.
C Program to Verify Leap Year Using a Function
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor18936.setValue(decodedContent); // Set the initial text
editor18936.clearSelection();
editor18936.setOptions({
maxLines: Infinity
});
function decodeHTML18936(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard18936() {
const code = editor18936.getValue(); // Fetch code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput18936() {
var code = editor18936.getSession().getValue();
jQuery(".maineditor18936 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn18936").addEventListener("click", copyCodeToClipboard18936);
document.getElementById("runBtn18936").addEventListener("click", runCode18936);
document.getElementById("closeoutputBtn18936").addEventListener("click", closeoutput18936);
Result:
The code demonstrates how a function “isLeapYear()” is created to determine a leap year using the divisibility criteria, returning the result from the main() function, which is then printed to the output.
C Program to Determine Leap Year Using While Loop
Cpp
Code Duplicated!
var isMobile = window.innerWidth ");
editor5729.setValue(decodedContent); // Set the initial text
editor5729.clearSelection();
editor5729.setOptions({
maxLines: Infinity
});
function decodeHTML5729(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard5729() {
const code = editor5729.getValue(); // Fetch code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput5729() {
var code = editor5729.getSession().getValue();
jQuery(".maineditor5729 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn5729").addEventListener("click", copyCodeToClipboard5729);
document.getElementById("runBtn5729").addEventListener("click", runCode5729);
document.getElementById("closeoutputBtn5729").addEventListener("click", closeoutput5729);
Output:
This code illustrates how the year 2016 is entered into a C program to verify whether it is a leap year or not by utilizing a while loop alongside divisibility criteria, subsequently displaying the output “2016 is a leap year” in the console.
C Program to Identify Leap Year Using a Switch Statement
Cpp
Code Copied!
var isMobile = window.innerWidth ");
editor38813.setValue(decodedContent); // Establish the default text
editor38813.clearSelection();
editor38813.setOptions({
maxLines: Infinity
});
function decodeHTML38813(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to copy code to clipboard
function copyCodeToClipboard38813() {
const code = editor38813.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
jQuery(".maineditor38813 .copymessage").show();
setTimeout(function() {
jQuery(".maineditor38813 .copymessage").hide();
}, 2000);
}).catch(err => {
console.error("Error copying code: ", err);
});
}
function runCode38813() {
var code = editor38813.getSession().getValue();
function closeoutput38813() {
var code = editor38813.getSession().getValue();
jQuery(".maineditor38813 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn38813").addEventListener("click", copyCodeToClipboard38813);
document.getElementById("runBtn38813").addEventListener("click", runCode38813);
document.getElementById("closeoutputBtn38813").addEventListener("click", closeoutput38813);
Output:
This code demonstrates how the year 2019 is supplied as input in a C program to ascertain if it is a leap year, employing a switch statement and divisibility conditions, then outputs the result “2019 is not a leap year” in the console.
C Program to Identify Leap Years Within a Specified Range
Cpp
Code Copied!
var isMobile = window.innerWidth
``````html
");
editor31754.setValue(decodedContent); // Assign the default text
editor31754.clearSelection();
editor31754.setOptions({
maxLines: Infinity
});
function decodeHTML31754(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
// Function to duplicate code to clipboard
function copyCodeToClipboard31754() {
const code = editor31754.getValue(); // Retrieve code from the editor
navigator.clipboard.writeText(code).then(() => {
// alert("Code copied to clipboard!");
function closeoutput31754() {
var code = editor31754.getSession().getValue();
jQuery(".maineditor31754 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn31754").addEventListener("click", copyCodeToClipboard31754);
document.getElementById("runBtn31754").addEventListener("click", runCode31754);
document.getElementById("closeoutputBtn31754").addEventListener("click", closeoutput31754);
Result:
The code demonstrates how the leap years are identified within a specified range between 2000 and 2025 utilizing the isLeapYear() function, adhering to the guidelines of determining a leap year, while the list of leap years is displayed in the console.
Conclusion
Comprehending a leap year is crucial for recognizing calendar adjustments in the typical number of days of a year. A leap year can also be ascertained manually with the application of divisibility principles. Various approaches in C can facilitate the identification of a leap year with ease. Therefore, having a clear understanding of what constitutes a leap year, alongside the diverse methods in C to determine a leap year, allows individuals to ascertain a leap year both manually and programmably using divisibility rules.
C Program to Verify Leap Year – FAQs
Q1. How can I manually verify if a year is a leap year?
You can manually determine if a year is a leap year by checking if the year is divisible by 4 and not divisible by 100, unless it is also divisible by 400.
Q2. Is the year 2100 regarded as a leap year?
No, 2100 does not qualify as a leap year as it is divisible by 4 and 100, yet not divisible by 400.
Q3. Is it possible to apply nested ternary operators for leap year verification?
Yes, nested ternary operators can be utilized for leap year verification, but employing a straightforward if-else statement is recommended for clarity and simplicity in programming.
Q4. Which technique is optimal for leap year verification in C?
The optimal technique to confirm a leap year is to implement a reusable function, as it renders your code cleaner, modular, and simpler to test.
Q5. How frequently does a leap year occur?
A leap year takes place every 4 years, except in cases of years that are divisible by 100 but not by 400.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.