Monday 18 May 2020

FINDING DAYS IN A MONTH WITH RESPECT TO YEAR USING JAVA LANGUAGE

Q-38- Write a java program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 had 31 days. 

INPUT :

import java.util.Scanner;

public class NCM {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int month, year;
System.out.println("Enter The Month And Year :");
month=sc.nextInt();
year=sc.nextInt();
if(month==1)
{
System.out.println("January "+year+" Had 31 Days.");
}
else if(month==2)
{
if((year%4==0) && (year%100!=0) || (year%400==0))
{
System.out.println("February "+year+" Had 29 Days.");
}
else
{
System.out.println("February "+year+" Had 28 Days.");
}
}
else if(month==3)
{
System.out.println("March "+year+" Had 31 Days.");
}
else if(month==4)
{
System.out.println("April "+year+" Had 30 Days.");
}
else if(month==5)
{
System.out.println("May "+year+" Had 31 Days.");
}
else if(month==6)
{
System.out.println("June "+year+" Had 30 Days.");
}
else if(month==7)
{
System.out.println("July "+year+" Had 31 Days.");
}
else if(month==8)
{
System.out.println("August "+year+" Had 31 Days.");
}
else if(month==9)
{
System.out.println("September "+year+" Had 30 Days.");
}
else if(month==10)
{
System.out.println("October "+year+" Had 31 Days.");
}
else if(month==11)
{
System.out.println("November "+year+" Had 30 Days.");
}
else if(month==12)
{
System.out.println("December "+year+" Had 31 Days.");
}
else
{
System.out.println("You Have Entered Wrong Input :");
}

}

}

OUTPUT :

Enter The Month And Year :
2
2012
February 2012 Had 29 Days.

Enter The Month And Year :
3
2015
March 2015 Had 31 Days.




No comments:

Post a Comment