- A year is said to be leap year if it is divisible by 4.
- For example 2004 , 2008,2012 and 2016.
- A year is also a leap year if it is divisible by 100 and divisible by 400.
- For example 1600 and 2000 etc.
- A leap year of February moth will have 29 days.
- Let us see an example C program to check a year is leap year or not using simple if condition.
- First we need to check a year is divisible by 4 or not.
- If yes then again one more condition if it is divisible by 100 and 400. if it is divisible by 100 then it should be divisible by 400 then only it is leap year.
- c program code to find leap year using if else
- #include <stdio.h>
- #include <stdlib.h>
- // Function to check if a year is a leap year
- int isLeapYear(int year) {
- // Logic for leap year:
- // A year is a leap year if:
- // 1. It is divisible by 4
- // 2. If divisible by 100, it must also be divisible by 400
- if (year % 4 == 0) {
- if (year % 100 == 0) {
- if (year % 400 == 0) {
- // Year is divisible by 400
- return 1; // It is a leap year
- } else {
- // Year is divisible by 100 but not by 400
- return 0; // Not a leap year
- }
- } else {
- // Year is divisible by 4 but not by 100
- return 1; // It is a leap year
- }
- } else {
- // Year is not divisible by 4
- return 0; // Not a leap year
- }
- }
- int main() {
- // Program to check leap year in C programming
- // Input year and output whether it is a leap year or not
- int year; // Variable to store year input by user
- printf("=== Leap Year Checker in C ===\n\n");
- printf("This program checks if a given year is a leap year or not.\n\n");
- // Prompt user for input
- printf("Enter a year to check: ");
- scanf("%d", &year);
- // Call function to check leap year
- if (isLeapYear(year)) {
- printf("%d is a leap year!\n", year);
- } else {
- printf("%d is not a leap year.\n", year);
- }
- // Demonstrating the leap year logic with examples
- printf("\nExamples of leap year logic:\n");
- printf("- 2000: Divisible by 400, so it is a leap year.\n");
- printf("- 1900: Divisible by 100 but not by 400, so not a leap year.\n");
- printf("- 2016: Divisible by 4 but not by 100, so it is a leap year.\n");
- printf("- 2019: Not divisible by 4, so not a leap year.\n\n");
- // Prompt for additional checks
- char choice;
- do {
- printf("Would you like to check another year? (y/n): ");
- scanf(" %c", &choice);
- if (choice == 'y' || choice == 'Y') {
- printf("\nEnter a year to check: ");
- scanf("%d", &year);
- // Call function again for the new input
- if (isLeapYear(year)) {
- printf("%d is a leap year!\n", year);
- } else {
- printf("%d is not a leap year.\n", year);
- }
- }
- } while (choice == 'y' || choice == 'Y');
- printf("\nThank you for using the Leap Year Checker in C program!\n");
- return 0;
- }
Output:
- === Leap Year Checker in C ===
- This program checks if a given year is a leap year or not.
- Enter a year to check: 2000
- 2000 is a leap year!
- Examples of leap year logic:
- - 2000: Divisible by 400, so it is a leap year.
- - 1900: Divisible by 100 but not by 400, so not a leap year.
- - 2016: Divisible by 4 but not by 100, so it is a leap year.
- - 2019: Not divisible by 4, so not a leap year.
- Would you like to check another year? (y/n): n
- Thank you for using the Leap Year Checker in C program!
Program #2: Write a C program to check a year is leap year or not without using any function.
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int year;
- printf("Enter a year to check if it is a leap year or not \n");
- scanf("%d", &year);
- if ( year%400 == 0)
- printf("%d is a leap year.\n", year);
- else if ( year%100 == 0)
- printf("%d is not a leap year.\n", year);
- else if ( year%4 == 0 )
- printf("%d is a leap year.\n", year);
- else
- printf("%d is not a leap year.\n", year);
- return 0;
- }
Output:
No comments