Since it is true, so the text mentioned “Given Year is a Leap Year” is displayed on the console. In the end, checking the value of the variable ‘isLeapYear’(which is a boolean variable holding true or false values).So the value of the boolean variable ‘isLeapYear’ becomes true. Since 2020 %100 != 0, we will move to the else part. Checking the even divisibility of 2020 by 100.Since 2020 %4 =0, we will move to the second step of the if statement. Checking the even divisibility of 2020 by 4.Checking the given year step by step according to the code written. Suppose we dry run the above code with an input value 2020. In the above code, we have implemented the above-mentioned logic with 3 steps using the if and else statements. Please find some of the snapshots of the outputs on executing the above program providing the different values of the years to check whether the year provided by the user is a leap year or not: Final checking the value of boolean variable ‘isLeapYear’ and displaying the final results on the console Checking the third condition of the year divisible by 100 and 400 both Checking the second condition of the century year (as we skip a leap year after every 100 years) Checking the first and foremost condition of leap year (" Please enter the year you want to test ") We have used the Scanner class to take the input from the user main method of java class from where the execution starts We have written the program of leap year in Java, taking the input from the user using the Scanner class. Step 5: Respective year entered by the user is not a leap year. Step 4: Respective year entered by the user is a leap year. Step 3: If the given year is evenly divisible by 400, go to step 4 else, go to step 5. Step 2: If the given year is evenly divisible by 100, go to step 3 else, go to step 4. Step 1: If the given year is evenly divisible by 4, go to step 2 else, go to step 5. Otherwise, logic will remain the same as mentioned above below given is the detailed algorithm implementing checking whether the given year is a leap year or not:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |