1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* Determine If Year Is Leap Year Java Example This Determine If Year Is Leap Year Java Example shows how to determine whether the given year is leap year or not. */ public class DetermineLeapYearExample { public static void main(String[] args) { //year we want to check int year = 2004; //if year is divisible by 4, it is a leap year if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) System.out.println("Year " + year + " is a leap year"); else System.out.println("Year " + year + " is not a leap year"); } } /* Output of the example would be Year 2004 is a leap year */ |
But is there a way of deleting a key pair using only the value?
Same question as above: the title is misleading, this doesn’t remove a value, this removes a key.
Comment # 1 -> Yes, it is possible to remove key-value using value. Search through the collection and when you find the element delete it using corresponding key.
Comment # 2 ->I do not agree. It deletes the value actually. Collections stores VALUES and associate a key to same for easy retrieval. So actually you are removing value using its key.
/*The logic of Leap Year Programis not complete
it should be*/
if(year%400==0)
System.out.println(year+” is a leap year”);
else if(year%4==0 && year%100!=0)
System.out.println(year+” is a leap year”);
else
System.out.println(year+” is not a leap year”);
The logic of leap year is wrong…. A year shouldalso be divisble by 400 to be a leap year…
line no 16 should be replaced by
if(year % 4 == 0 && year % 400 == 0)
the only mistake in this code is:
they have written
if (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
instead of
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
can the year be accepted as a parameter also?
it should be
if ((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0))