JAVA Days In A Year (no if statement) Write a program named DaysInYearNoIF that
ID: 3727490 • Letter: J
Question
JAVA
Days In A Year (no if statement)
Write a program named DaysInYearNoIF that reads an integer representing a year. (In this case there is no prompt from the program.) The program prints out the number of days in that year (366 if the year is a leap year, 365 otherwise).
CONSTRAINT: Nowhere in the program is an if statement used.
REMINDER: the program's output is shown here in bold; the user's data entry is shown here in italics.
Sample Interactive Run 1:
2015
365 days
Sample Interactive Run 2:
1992
366 days
Explanation / Answer
import java.util.Scanner;
public class DaysInYearNoIF {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int days = 365;
days = days + (((year % 400 == 0) || (year%4 == 0 && year%100 != 0)) ? 1 : 0);
System.out.println(days+" days");
}
}
/*
Sample run:
2015
365 days
1992
366 days
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.