C Programming Examples Tutorial Index

C String Programs

C program code for Leap Year, this C program is used to check whether the given year of input is a leap year or not.



Program:

#include <stdio.h>

int main()
{    
    int year;      
    printf("Enter a year to check if it is a leap year\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;    
}

Program Output:

leap-year-c

Explanation:

This program is used to check whether the given year of input is a leap year or not. So first of all, you have to include the stdio header file using the "include" preceding # which tells that the header file needs to be process before compilation, hence named preprocessor directive.

Then you have to define the main() function and it has been declared nothing so by default it returns integer. Now inside the main() you have to declare an integer type variable year. Then using printf() you have to display a message - "Enter a year to check if it is a leap year". Then the scanf() is used to take input from the user and store it in variable 'year'. Then you have to put a condition that whether year%400 is equal to 0; if yes, prints that the given year is a leap year. Using else if, another condition is checked whether year%100 equals to 0 or not, prints, the year is not a leap year. Again another condition is checked whether year%4 is equaled to 0 or not if the condition is true, printf() prints the message - given year is a leap year or else prints the year is not a leap year.

And lastly the return 0; statement is used to return an integer type value back to main().



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram