Write a program to find Number of days in a given month of a given year

04 May 2022

Write a program to find Number of days in a given month of a given year


Description

Get the number of month and year as input from the user and check the number of days present in that month.

Input

Enter month: 2

Enter year: 2000

Output

29


C Program

#include<stdio.h> 

int main()

{

    int month, year;

    printf("enter month : ");

    scanf("%d",&month);

    printf("enter year : ");

    scanf("%d",&year);

    if(((month==2) && (year%400==0)) || ((year%100!=0)&&(year%4==0)))

    {

        printf("Number of days is 29");

    }

    else if(month==2)

    {

        printf("Number of days is 28");

    }

    else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

    {

        printf("Number of days is 31");

    }

    else

    {

        printf("Number of days is 30");

    }

    return 0;

}


C++ Program

#include <iostream>

using namespace std;

int main()

{

    int month, year;

    cout<<"enter month : ";

    cin>>month;

    cout<<"enter year : ";

    cin>>year;

    if(((month==2) && (year%400==0)) || ((year%100!=0)&&(year%4==0)))

    {

        cout<<"Number of days is 29";

    }

    else if(month==2)

    {

        cout<<"Number of days is 28";

    }

    else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

    {

        cout<<"Number of days is 31";

    }

    else

    {

        cout<<"Number of days is 30";

    }

    return 0;

}


Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

int month, year;

    Scanner sc = new Scanner(System.in);

    System.out.println("enter the month and year: ");

    month=sc.nextInt();

    year=sc.nextInt();

    if(((month==2) && (year%400==0)) || ((year%100!=0)&&(year%4==0)))

    {

        System.out.println("Number of days is 29");

    }

    else if(month==2)

    {

        System.out.println("Number of days is 28");

    }

    else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)

    {

        System.out.println("Number of days is 31");

    }

    else

    {

        System.out.println("Number of days is 30");

    }

}

}


Python

month = int(input('Enter the month: '))

year = int(input('Enter the year: '))

if(month == 2 and (year%400 == 0) or ((year%100 != 0) and (year%4 == 0))):

    print('Number of days is 29')

elif(month == 2):

    print('Number of days is 28')

elif(month == 1 or Month == 3 or Month == 5 or Month == 7 or Month == 8 or Month == 10 or Month == 12):

    print('Number of days is 31')

else:

    print('Number of days is 30')

Related Articles

Ask Us Anything !