Leap year program in C, C++ and Java | Program to check if a given year is a leap year or not

In this article, we will discuss the leap year program in C with and without using command line arguments.

Problem:

Write a c program, to check whether the given year is a leap year or not. A leap year is a calendar year containing one additional day (Feb 29th) added to keep the calendar year synchronized with the astronomical year.

Flowchart for leap year program

flowchart of leap year program in c

Leap year program in C | Solution

#include
int main(int a, char*b[])
{
int year; year=atoi(b[1]);
if(year%100==0)
{
if(year%400==0)
{
printf(“LEAP YEAR”);
}
else{
printf(“NOT LEAP YEAR”); } }
else if(year%4==0)
{
printf(“LEAP YEAR”);
}
else{
printf(“NOT LEAP YEAR”);
}
return 0; }

Leap year program in C

#include <stdio.h>

int main()
{
int year;
printf(“Enter a year: “);
scanf(“%d”,&year);
printf(“\n”);
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);
printf(“\n”);
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int year;
cout << “Enter a year: “;
cin >> year;
cout << endl;
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
cout << year << ” is a leap year” ;
else
cout << year << ” is not a leap year”;
}
else
cout << year << ” is a leap year”;
}
else
cout << year << ” is not a leap year”;
cout << endl;
return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args) {
int year;
System.out.print(“Enter a year: “);
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
System.out.println(“\n”);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
System.out.println(year + ” is a leap year\n”);
else
System.out.println(year + ” is not a leap year\n”);
}
else
System.out.println(year + ” is a leap year\n”);
}
else
System.out.println(year + ” is not a leap year\n”);
}

}

year = int(input(“Enter a year : “))
if(year%4 == 0):
    if( year%100 == 0):
        if ( year%400 == 0):
            print(year, end = ”)
            print(” is a leap year”)
        else:
            print(year, end = ”)
            print(” is not a leap year”)
    else:
        print(year, end = ”)
        print(” is a leap year”)
else:
    print(year, end = ”)
    print(” is not a leap year”)

Leap year program in C using functions

#include <stdio.h>

void is_leap_year(int year)
{
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);
printf(“\n”);
}

int main()
{
int year;
printf(“\nEnter a year : “);
scanf(“%d”,&year);
printf(“\n”);
is_leap_year(year);
return 0;
}

#include <iostream>
using namespace std;

void is_leap_year(int year)
{
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
cout << year << ” is a leap year” ;
else
cout << year << ” is not a leap year”;
}
else
cout << year << ” is a leap year”;
}
else
cout << year << ” is not a leap year”;
cout << endl;
}

int main()
{
int year;
cout << “Enter a year: “;
cin >> year;
cout << endl;
is_leap_year(year);
return 0;
}

import java.util.*;
public class Main
{
static void is_leap_year(int year)
{
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
System.out.println(year + ” is a leap year\n”);
else
System.out.println(year + ” is not a leap year\n”);
}
else
System.out.println(year + ” is a leap year\n”);
}
else
System.out.println(year + ” is not a leap year\n”);
}

public static void main(String[] args) {
int year;
System.out.print(“Enter a year: “);
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
System.out.println(“\n”);
is_leap_year(year);
}

}

def is_leap_year(year):
    if(year%4 == 0):
        if( year%100 == 0):
            if ( year%400 == 0):
                print(year, end = ”)
                print(” is a leap year”)
            else:
                print(year, end = ”)
                print(” is not a leap year”)
        else:
            print(year, end = ”)
            print(” is a leap year”)
    else:
        print(year, end = ”)
        print(” is not a leap year”)

year = int(input(“Enter a year : “))
is_leap_year(year)

leap year program in c

leap year program in c