diff --git a/src/navs/program.js b/src/navs/program.js index 0bdd08e3..5de23cf8 100644 --- a/src/navs/program.js +++ b/src/navs/program.js @@ -14,5 +14,6 @@ export const programsNav = { pages['java-program-to-add-two-binary-numbers'], pages['java-program-to-add-two-complex-numbers'], pages['multiply-two-numbers'], + pages['java-program-to-check-Leap-year'], ], } diff --git a/src/pages/programs/java-program-to-check-Leap-year.mdx b/src/pages/programs/java-program-to-check-Leap-year.mdx new file mode 100644 index 00000000..135f5bdb --- /dev/null +++ b/src/pages/programs/java-program-to-check-Leap-year.mdx @@ -0,0 +1,61 @@ +--- +title: Java Program to check Leap year +ShortTitle: Check Leap year +description: In this program, you'll learn to check wheather the given Year (integer) is a leap year or not. +--- + +import { TipInfo } from '@/components/Tip' + +To understand this example, you should have the knowledge of the following Java programming topics: +- [Java Operator](/docs/operator) +- [Java Basic Input and Output](/docs/basic-input-output) + +## Check Leap year + +A java program that checks wheather the given year is leap year or not is as follows: + +```java +import java.util.Scanner; + +class checkLeapYear{ + public static void main(String[] args){ + int year; + boolean isLeapYear; + System.out.print("Enter the year to check: "); + // instance of scanner class + Scanner input = new Scanner(System.in); + // taking user input in year variable + year = input.nextInt(); + // checking weather the year is leap or not + isLeapYear = year % 4 == 0; + + if(isLeapYear){ + System.out.println(year + " is leap year"); + }else{ + System.out.println(year + " isn't leap year"); + } + input.close(); + } +} +``` + +#### Output 1 + +```text +Enter the year to check: 2003 +2003 isn't leap year +``` +#### Output 2 +```text +Enter the year to check: 2004 +2004 is leap year +``` + +To check weather a `year` is `leap` or not we only need to check weather it's `divisible by 4` or not if it doesn't leave reminder then leap year else not a leap year + + +Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input) + +Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method). + + \ No newline at end of file