Program to find leap year or not using function

 

Problem Statement:- Program to check whether the given year is a leap year or not using function.


Sample Input/Output:-


Sample Input First:

2016

Sample Output First: 

Leap Year

Sample Input Second: 

2015

Sample Output Second: 

Not Lear year


Data requirement:-


  Input Data:- year


  Output Data:-string output


Program in C

Here is the source code of the C program to find whether an entered year is a leap year or not using function.


Code:

#include <stdio.h>/*printf,scanf definitions */
/* This function checking given year is leap year or not*/
int check_leap_year(int year)
{
    if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
        return 1;
    else
        return 0;
}
int main()
{
    int year;
    /* Get the year input*/
    printf("Enter the year:");
    scanf("%d", &year);
    /* Display the given year is leap year or not*/
    check_leap_year(year) ? printf("Given Year is a Leap Year.") : 
    printf("Given year is not a Leap Year.");
    
    /* ***Another way*** 
    if(check_leap_year(year))
        printf("Given Year is a Leap Year.\n");
    else
        printf("Given year is not a Leap Year.");
    */

}


Input/Output:

Enter the year:2015

Given year is not a Leap Year.


Program in C++

Here is the source code of the C++ Program to check whether the given year is a leap year or not using function.


Code:

#include <iostream>
using namespace std;
/* This function checking given year is leap year or not*/
int CheckLeapYear(int year)
{
    if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
        return 1;
    else
        return 0;
}
int main()
{
    int year;
    /* Get the year input*/
    cout<<"Enter the year:";
    cin>>year;
    /* Display the given year is leap year or not*/
    CheckLeapYear(year) ? cout<<"Given Year is a Leap Year."
    cout<<"Given year is not a Leap Year.";

}


Input/Output:

Enter the year:2016

Given Year is a Leap Year.


Program in Java

Here is the source code of the Java program to find whether an entered year is a leap year or not using function.


Code:

import java.util.Scanner;

public class LeapYear {
    /* This function checking given year is leap year or not */
    static boolean checkLeapYear(int year) {
        if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
            return true;
        else
            return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year;
        /* Get the year input */
        System.out.println("Enter the year:");
        year = sc.nextInt();
        /* Display the given year is leap year or not */
        System.out.println(checkLeapYear(year? "Given Year is a Leap Year." : "Given year is not a Leap Year.");
        sc.close();

    }
}


Input/Output:

Enter the year:

2019

Given year is not a Leap Year.


Program in Python

Here is the source code of the Python Program to check whether the given year is a leap year or not using function.


Code:

""" This function checking given year is leap year or not """
def check_leap_year(year):
    if ((year % 100 == 0 and year % 400 == 0or (year % 100 != 0 and year % 4 == 0)):
        return 1
    else:
        return 0

""" Get the year input """
year = int(input("Enter the year:"))
""" Display the given year is leap year or not """
if(check_leap_year(year)):
    print("Given Year is a Leap Year.")
else:
    print("Given Year is not a Leap Year.")


Input/Output:

Enter the year:2012

Given Year is a Leap Year.

Most Recommend Questions:-







More Questions:-


Post a Comment

0 Comments