Find the Month and Day.
Working with 2 parallel arrays. One holds the months of the years, the other array holds the # of days in each month.
This lab will determine what Month and DayOfMonth for any given day of the year.
1). Name the lab "Lab4_MonthAndDay". Remember, case sensistive.
2). Ask user to input a # between 1 and 365. Validate the input.
3). Also ask user to input the year, between year 1 and year 202. Validate the input.
3). Use the input to determine the month, and the day of the month. For example, day 32 is February 1st. Day 70 is March 11.
in Java
import java.util.*;
public class Day_month {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter a year between 2001 and 2020");
int year= sc.nextInt();
if(year>=2001 && year<= 2020){
if(year%4==0 && year%100==0 && year%400==0){
int []days= {31,29,31,30,31,30,31,31,30,31,30,31};
System.out.println("Enter days between 1 to 365");
int day = sc.nextInt();
if(day>=1 && day<=366)
Lab4_MonthAndDay(day, days);
}
else
{
int []days= {31,28,31,30,31,30,31,31,30,31,30,31};
System.out.println("Enter days between 1 to 365");
int day = sc.nextInt();
if(day>=1 && day<=365)
Lab4_MonthAndDay(day, days);
}
}
}
public static void Lab4_MonthAndDay(int day, int []days){
String []months= {"January","February","March","April","May","June","July","August","September","October","November","December"};
int copy= day;int i=0;
for(i=0; i<12; i++){
if(day> days[i])
day= day- days[i];
else
break;
}
System.out.println("Day "+copy+" is "+months[i]+" "+day);
}
}
Get Answers For Free
Most questions answered within 1 hours.