Write a program that prints the day number of the year, given the date in the fo
ID: 3582422 • Letter: W
Question
Write a program that prints the day number of the year, given the date in the form month-day-year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400.Explanation / Answer
public class Date_Formate {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int day = 0;
System.out.println("Enter The Date In The Formate Of 'month-day-year'");
String str;
Scanner sc = new Scanner(System.in);
str = sc.next();
int len = str.length();
String yr = str.substring(str.length() - 4);
System.out.println(yr);
int yr1 = Integer.parseInt(yr);
if ((yr1 % 400 == 0) || ((yr1 % 4 == 0) && (yr1 % 100 != 0))) {
day = 366;
} else {
day = 365;
}
int i = 0, j = 1, counter = 1;
for (; i < str.length(); i++) {
if ((str.charAt(i) == '-')) {
break;
}
}
j = ++i;
while (str.charAt(i) != '-') {
i++;
}
String day1 = str.substring(j, i);
day = day + Integer.parseInt(day1);
System.out.println(day1);
System.out.println(" This Is Day Number " + day + " Of The Year " + yr1);
}
}
Hi Friend in thsi question you not mentioned in which programming language you need your code so i used Java Code ,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.