Quizzes Assignments Puzzles Easy exercises Required knowledge |
Java Assignments Green = Easy, Blue = Normal, Red = Hard Select 01 02 03 04 05 06 07 08 09 10 By title |
Write a program that allows the user to enter a year. After inserting a year and pressing the enter button, the program determines whether the inserted year is a leap year.
Note: A leap year is divisible by 4 with no remainder.
Java assignment 07: Find the leap years
Level: Normal
import java.util.Scanner; public class CheckYear { public boolean isLeapYear(int year) { // write your code here } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Insert a year: "); int insertedYear = input.nextInt(); CheckYear cy = new CheckYear(); if(cy.isLeapYear(insertedYear)) { System.out.println(insertedYear + " is a leap year"); } else { System.out.println(insertedYear + " is not a leap year"); } input.close(); } } /* Below, the user inserted the year 2020 Insert a year: 2020 2020 is a leap year */
Author: Sar Maroof
Answer explanation
The leap years from 2021 till 2053 are 2024 2028 2032 2036 2040 2044 2048 2052.
The following code writes the leap years mentioned above.
import java.util.Scanner; public class CheckYear { public boolean isLeapYear(int year) { if(year % 4 == 0) { return true; } else { return false; } } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Insert a year: "); int insertedYear = input.nextInt(); CheckYear cy = new CheckYear(); if(cy.isLeapYear(insertedYear)) { System.out.println(insertedYear + " is a leap year"); } else { System.out.println(insertedYear + " is not a leap year"); } input.close(); } }
Suggested Articles
![]() ![]() ![]() |