On Earth, a year lasts 365 or 366 days. The number of days is calculated according to the following formula: A leap year (366 days) is every year evenly divisible by 4, except for years that are multiples of 100 but not multiples of 400. We'll write a program that will determine whether the user has entered a leap-year or ordinary year from the keyboard.
Number of days in the year
- 8
Locked
Comments (33)
- Popular
- New
- Old
You must be signed in to leave a comment
Daisy
30 May 2021, 09:52
It can be solved without the operator %
hint: when int a can be evenly divisible by int b, a==(a/b)*b.Then you can nail it with if/else!
0
ImDevin
29 March 2021, 21:37
Number of days in the year: x
, where
x is 366 for a leap year, and
x is 365 for an ordinary year.
all this means is they want you to hard-code the numbers in! I was putting a variable x and x++, thus failing the test over and over again! what a STUPID waste of time! :/
0
Seth Barker
30 January 2021, 23:05
I created code to perform all the functions, however, I was leaving off the #4 task. The line of code that covered "all the remaining years"
0
Libby
10 January 2021, 18:12
For some reason, the help section won't accept my question. Can anyone help me out here? The third requirement is not met.
package com.codegym.task.task04.task0414;
/*
Number of days in the year
*/
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String y = bufferedReader.readLine();
int a = Integer.parseInt(y);
int leap400 = a % 400;
int nonLeap = a % 100;
int leap4 = a % 4;
if (a == leap400)
{
System.out.println("Number of days in the year: 366");
}
else if (a == nonLeap)
{
System.out.println("Number of days in the year: 365");
}
else if (a == leap4)
{
System.out.println("Number of days in the year: 366");
}
else
{
System.out.println("Number of days in the year: 365");
}
}
}
0
Roman
11 January 2021, 07:02
You received the recommendation "Check the solution with different input values, for example: 1988"!
Your solution says "Number of days in the year: 365" for 1988.
But 1988 is a leap year and has 366 days!
+1
Libby
3 January 2021, 08:27
Third requirement not met. They don't allow me to post my question in the help section. It says "Invalid request argument"
0
Roman
4 January 2021, 09:12
Please check if all fields are filled in when posting a question in the help section. If there is still an error, contact support.
0
Albert
20 November 2020, 03:18
// I skipped the task with frustration and returned with ease after learning "%"
+2
Mihai Bone
30 September 2020, 00:56
I did waste 13 tries to get tis right.
0
Karas Java Developer
25 August 2020, 18:46
Every year that is evenly divisible by 4 is a leap year, unless they are evenly divisible by 100, in which case they also need to be evenly divisible by 400.
0
Richard Caputo
3 September 2021, 21:40
This is exactly the math explanation I needed, thanks
0
Ivan
22 June 2020, 11:39
This Statement "if the year is evenly divisible by 400, then it is a leap year" is false coz 2004 / 400 = 5.01
0
punkuotukas
25 June 2020, 16:11
But it's still divisible by 4.
+1
Ivan
25 June 2020, 16:29
I just didn't get conditions of the task at once)
+1
Liew
16 April 2020, 07:20
follow the steps from the hint it is very helpful.e.g. 4 steps then use 4 if else statements.
0