• 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


Program #1: Write a C program to check a year is leap year or not 


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. // Function to check if a year is a leap year

  4. int isLeapYear(int year) {
  5.     // Logic for leap year:
  6.     // A year is a leap year if:
  7.     // 1. It is divisible by 4
  8.     // 2. If divisible by 100, it must also be divisible by 400

  9.     if (year % 4 == 0) {
  10.         if (year % 100 == 0) {
  11.             if (year % 400 == 0) {
  12.                 // Year is divisible by 400

  13.                 return 1; // It is a leap year
  14.             } else {
  15.                 // Year is divisible by 100 but not by 400

  16.                 return 0; // Not a leap year
  17.             }
  18.         } else {
  19.             // Year is divisible by 4 but not by 100

  20.             return 1; // It is a leap year
  21.         }
  22.     } else {
  23.         // Year is not divisible by 4

  24.         return 0; // Not a leap year
  25.     }
  26. }

  27. int main() {

  28.     // Program to check leap year in C programming
  29.     // Input year and output whether it is a leap year or not

  30.     int year; // Variable to store year input by user

  31.     printf("=== Leap Year Checker in C ===\n\n");
  32.     printf("This program checks if a given year is a leap year or not.\n\n");

  33.     // Prompt user for input
  34.     printf("Enter a year to check: ");
  35.     scanf("%d", &year);

  36.     // Call function to check leap year
  37.     if (isLeapYear(year)) {

  38.         printf("%d is a leap year!\n", year);

  39.     } else {

  40.         printf("%d is not a leap year.\n", year);

  41.     }

  42.     // Demonstrating the leap year logic with examples
  43.     printf("\nExamples of leap year logic:\n");
  44.     printf("- 2000: Divisible by 400, so it is a leap year.\n");
  45.     printf("- 1900: Divisible by 100 but not by 400, so not a leap year.\n");
  46.     printf("- 2016: Divisible by 4 but not by 100, so it is a leap year.\n");
  47.     printf("- 2019: Not divisible by 4, so not a leap year.\n\n");

  48.     // Prompt for additional checks
  49.     char choice;
  50.     do {
  51.         printf("Would you like to check another year? (y/n): ");
  52.         scanf(" %c", &choice);

  53.         if (choice == 'y' || choice == 'Y') {
  54.             printf("\nEnter a year to check: ");
  55.             scanf("%d", &year);

  56.             // Call function again for the new input
  57.             if (isLeapYear(year)) {
  58.                 printf("%d is a leap year!\n", year);
  59.             } else {
  60.                 printf("%d is not a leap year.\n", year);
  61.             }
  62.         }
  63.     } while (choice == 'y' || choice == 'Y');

  64.     printf("\nThank you for using the Leap Year Checker in C program!\n");

  65.     return 0;
  66. }

 
Output:

  1. === Leap Year Checker in C ===

  2. This program checks if a given year is a leap year or not.

  3. Enter a year to check: 2000
  4. 2000 is a leap year!

  5. Examples of leap year logic:
  6. - 2000: Divisible by 400, so it is a leap year.
  7. - 1900: Divisible by 100 but not by 400, so not a leap year.
  8. - 2016: Divisible by 4 but not by 100, so it is a leap year.
  9. - 2019: Not divisible by 4, so not a leap year.

  10. Would you like to check another year? (y/n): n

  11. 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.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.     int year;
  7.     printf("Enter a year to check if it is a leap year or not \n");
  8.     scanf("%d", &year);
  9.  
  10.     if ( year%400 == 0)      
  11.     printf("%d is a leap year.\n", year);     
  12.     else if ( year%100 == 0)      
  13.     printf("%d is not a leap year.\n", year); 
  14.          
  15.     else if ( year%4 == 0 )      
  16.     printf("%d is a leap year.\n", year);
  17.     
  18.     else      
  19.     printf("%d is not a leap year.\n", year); 
  20.  
  21.   return 0;
  22.     
  23. }


 Output:


leap year in c program


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu