Open In App

Program to print the number of days in a given year

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Write a program to print the number of days in a given year.

Examples:

Input: 2023
Output: 365
Explanation: The year 2023 is not a leap year, therefore the number of days are 365.

Input: 2024
Output: 366
Explanation: The year 2024 is a leap year, therefore the number of days are 366.

Approach: To solve the problem, follow the below idea:

To determine the number of days in a given year, you need to consider whether the year is a leap year or not. A year is a leap year if the following conditions are satisfied: 

  • The year is multiple of 400.
  • The year is a multiple of 4 and not a multiple of 100.

So, if it is a leap year the year contains 366 days and if not a leap year, then no. of days in a year are 365.

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
bool isLeapYear(int year)
{
    // Leap year is divisible by 4, but not divisible by 100
    // unless divisible by 400
    return (year % 4 == 0 && year % 100 != 0)
        || (year % 400 == 0);
}
 
int main()
{
    int year = 2024;
 
    // Check if the year is a leap year
    if (isLeapYear(year)) {
        cout << "Leap year. It has 366 days." << endl;
    }
    else {
        cout << "Common year. It has 365 days." << endl;
    }
 
    return 0;
}


Java




public class LeapYearChecker {
 
    // Function to check if a year is a leap year
    static boolean isLeapYear(int year) {
        // Leap year is divisible by 4, but not divisible by 100 unless divisible by 400
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
 
    public static void main(String[] args) {
        // Set the year to be checked
        int year = 2024;
 
        // Check if the year is a leap year
        if (isLeapYear(year)) {
            System.out.println("Leap year. It has 366 days.");
        } else {
            System.out.println("Common year. It has 365 days.");
        }
    }
}


Python3




def is_leap_year(year):
    # Leap year is divisible by 4, but not divisible by 100
    # unless divisible by 400
    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
 
if __name__ == "__main__":
    year = 2024
 
    # Check if the year is a leap year
    if is_leap_year(year):
        print(f"Leap year. It has 366 days.")
    else:
        print(f"Common year. It has 365 days.")


C#




using System;
 
class LeapYearCheck
{
    // Function to check if a year is a leap year
    static bool IsLeapYear(int year)
    {
        // Leap year is divisible by 4, but not divisible by 100
        // unless divisible by 400
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
 
    static void Main()
    {
        int year = 2024;
 
        // Check if the year is a leap year
        if (IsLeapYear(year))
        {
            Console.WriteLine("Leap year. It has 366 days.");
        }
        else
        {
            Console.WriteLine("Common year. It has 365 days.");
        }
    }
}


Javascript




// Function to check if a year is a leap year
function is_leap_year(year) {
    // Leap year is divisible by 4, but not divisible by 100 unless divisible by 400
    return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
 
// Main Program
const year = 2024;
 
// Check if the year is a leap year
if (is_leap_year(year)) {
    console.log(`Leap year. It has 366 days.`);
} else {
    console.log(`Common year. It has 365 days.`);
}


Output

Leap year. It has 366 days.

Time Complexity: O(1)
Auxiliary space: O(1)



Similar Reads

Build an Android App to Check a Year is Leap Year or Not
In this article, we will learn how to make an App to check a year is a leap year or not. Before that, we need to have an idea of what is a leap year? If a year is: Multiple of 400multiple of 4 and not multiple of 100 Then the year is a leap year. Algorithmif the year is divisible by 400 then it is a leap year.else if the year is divisible by 100 th
3 min read
Program to find the number of days in a given month
Write a program to find the number of days in a given month. We are given name of a month in lowercase letters, so print the number of days in the month. Note: For February, consider the number of days as 28. Examples: Input: month = "january"Output: 31Explanation: January has 31 days. Input: month = "february"Output: 28Explanation: February has 28
3 min read
Program to count number of days between two given months
Write a program to count the number of days between two given months (including start as well as end month). Assume the number of days in February as 28. Examples: Input: startMonth = "january", endMonth ="january"Output: 31Explanation: January has 31 days. Input: startMonth = "january", endMonth = "july"Output: 212Explanation: 31 + 28 + 31 + 30 +
6 min read
Program to count the number of days between two years
Given two years. Write a program to count the number of days between the two years. Examples: Input: startYear = 1990, endYear = 2001Output: 4383 Explanation: Years 1992, 1996 and 2000 are leap year and all 9 others are non-leap years. So, total number of days are: 366 * 3 + 365 * 9 = 4383 Input: startYear = 1890, endYear = 1998Output: 39811 Approa
4 min read
Print Happy New Year 2024
Write a program to print "HAPPY NEW YEAR 2024" pattern. Approach to print Happy New Year 2024:Here are step-by-step instructions to create a program that prints "HAPPY NEW YEAR 2024" in a pattern: Set up your development environment: Ensure that you have a compiler installed on your machine. You can use an integrated development environment (IDE) l
9 min read
Program to convert days to weeks
Write a program to convert a given number of days to weeks. Examples: Input: 14 daysOutput: 2 weeks Input: 30 daysOutput: 4 weeks and 2 days Approach: To solve the problem, follow the below idea: Divide the given number of days by 7 to get the number of weeks. The remainder will be the remaining days. Step-by-step algorithm: Read the number of days
2 min read
Program to convert weeks to days
Given no of weeks. Write a program to convert weeks to days. Examples: Input: weeks = 4Output: 28 days Input: weeks = 2Output: 14 days Approach: To solve the problem, follow the below idea: Since we know a week has 7 days. So, to find no of days in a week, we multiply weeks by 7. 1 week = 7 daysn weeks = n*7 days Below is the implementation of the
3 min read
Find Days Between Two Dates in Android
There are many travels and accommodation-based Android applications which we use to check and buy tickets for any traveling and hotel booking. Inputs such as origin, traveling destination, selection of hotel, and trip dates are needed to book appropriate options. In the case of hotel booking, the number of days is calculated by finding the differen
3 min read
100 Days of Android Development: A Complete Guide For Beginners
In the dynamic world of Mobile application development mastering in developing applications is not just a skill but rather a journey. Welcome to the 100 days of Android Development journey where we will dive into different Android development important components day by day. The definition of Android Development of Android to the advanced Android c
7 min read
Implementation of Facebook’s Android Device Year Class Library in Android
As developers, we always aim to create an application that would run smoothly on millions of devices, however, the performance of practically every application is influenced by the device specifications. There are currently over 16,000 handsets on the market with varying specifications, and the application may perform poorly on some devices when co
3 min read
Practice Tags :