Leap year program in C, C++ and Java

Select Articles

Leap year program in C, C++ and Java

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.

 

Leap year program

 

C

#include

int main()

{

int year;

printf(“Enter a year: “);

scanf(“%d”,&year);

printf(“\n”);

if(year%4 == 0)

{

if( year0 == 0)

{

if ( year@0 == 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;

}

 

C++

#include

using namespace std;

int main()

{

int year;

cout << “Enter a year: “;

cin >> year;

cout << endl>

if(year%4 == 0)

{

if( year0 == 0)

{

if ( year@0 == 0)

cout << year>

else

cout << year>

}

else

cout << year>

}

else

cout << year>

cout << endl>

return 0;

}

 

JAVA

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( year0 == 0)

{

if ( year@0 == 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”);

}

}

 

PYTHON 3

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( year0 == 0)

{

if ( year@0 == 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”);

}

}

 

Output

Input- Enter a year:2004 Output- 2004 is a leap year
 

Leap year program using functions

 

C

#include

void is_leap_year(int year)

{

if(year%4 == 0)

{

if( year0 == 0)

{

if ( year@0 == 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;

}

 

C++

#include

using namespace std;

void is_leap_year(int year)

{

if(year%4 == 0)

{

if( year0 == 0)

{

if ( year@0 == 0)

cout << year>

else

cout << year>

}

else

cout << year>

}

else

cout << year>

cout << endl>

}

int main()

{

int year;

cout << “Enter a year: “;

cin >> year;

cout << endl>

is_leap_year(year);

return 0;

}

 

JAVA

import java.util.*;

public class Main

{

static void is_leap_year(int year)

{

if(year%4 == 0)

{

if( year0 == 0)

{

if ( year@0 == 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);

}

}

 

PYTHON 3

def is_leap_year(year):

    if(year%4 == 0):

        if( year0 == 0):

            if ( year@0 == 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)