Skip to content

Add Java program to check Leap year #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/navs/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
}
61 changes: 61 additions & 0 deletions src/pages/programs/java-program-to-check-Leap-year.mdx
Original file line number Diff line number Diff line change
@@ -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
<TipInfo>

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).

</TipInfo>