Check value

Program to check leap year in C language

Program to check leap year in C language

In this article, we will discuss the concept of Program to check leap year in C language

In this post, we are going to learn how to write a program to check and display  the given year is leap or not in  C programming language.

leap year

What is leap year

leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year

Ex – 2000,2004.2008

 

Century year

The century year is ending with 00

Ex 2000, 2100, 2200

a century year may be a leap year(only if the year is exactly divisible by 400)

 

How to find the leap year

if The given year is perfectly divisible by 4 , it is a leap year(except century year)

only if is perfectly divisible by 400, the century year is a leap year,
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4

 

Code to check leap year using if else

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else statements in C language

Program 1

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

int main()
{
    int year;
//variable declaration
    printf("Enter a year for check leap or not\n");
    //Ask input from the user
    scanf("%d",&year);
//store  the input in the year variable
   if ((year%400==0)||((year%4==0)&&(year%100!=0))){
     printf("%d is a leap year ",year);
}//display leap year
       else{
       printf("%d is not a leap year ",year);
}
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2404
2404 is a leap year

 

Case 2

Enter a year for check leap or not
2200
2200 is not a leap year

 

 

Code to check leap year using if else if

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else-if else  statements in C language

Program 2

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

int main()
{
    int year;
    printf("Enter a year for check leap or not\n");
    //ask input from the user
    scanf("%d",&year);
    //store the year in the years variable entered by user
    if(year%400==0){// if the year is  perfectly divisible by 400
            //it is a leap year
     printf("%d is a leap year ",year);
}//if the year is divisible by 100 , it is not leap year
else if(year%100==0){
    printf("%d is not a leap year ",year);
}
//if a year is divisible by 4 but not divisible by 100
//it is a leap year
   else if(year%4==0){
        printf("%d is a leap year ",year);
   }
       else{
       printf("%d is not a leap year ",year);
}
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2400
2400 is a leap year

 

Case 2

Enter a year for check leap or not
2500
2500 is not a leap year

 

Code to check leap year using Nested if

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using Nested if – else statements in C language

Program 3

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

int main()
{
    int year;//variable declaration

    printf("Enter a year for check leap or not\n");
    //Ask input from the user
     scanf("%d",&year);
//store the value in the year variable
   if(year%4==0){
     if(year%100==0){
           if(year%400==0){
     printf("%d is a leap year ",year);
        }else
    printf("%d is not a leap year ",year);
    } else
    printf("%d is a leap year ",year);
  }else
       printf("%d is not a leap year ",year);
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2020
2020 is a leap year

 

Case 2

Enter a year for check leap or not
2700
2700 is not a leap year

 

Code to check leap year using ternary operator

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not using Ternary operator in C language

Program 4

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

int main()
{
    int year;//variable declaration
    printf("Please Enter year for check leap: \n");
    //Ask input from the user
    scanf("%d",&year);
    //Store the input in the year variable
    (year%4==0 && year%100!=0)? printf("The entered year is a leap year"):(year%400==0)?
    printf("The entered year is a leap year")
:printf("This is not a leap year");
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter a year for check leap or not
2024
The entered year is a leap year

 

Case 2

Enter a year for check leap or not
2022
The entered year is not a leap year

 

Suggested post

if statements inn C language

Nested if statements in C language

Data type in C language

Variable in C language

Operator in C language

input output function in C language

 

Similar post

Java program to find leap year

C program to find leap year

C++ program to find leap year

Python program to find leap year

 

 

Java program to check the given year is a leap year
Program to check if the given year is leap year in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago