Skip to content

Commit cb66ce5

Browse files
Kapilrohillauiuxarghya
authored andcommitted
create java-program-to-check-Leap-year.mdx
1 parent a6097c7 commit cb66ce5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/navs/program.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ export const programsNav = {
1414
pages['java-program-to-add-two-binary-numbers'],
1515
pages['java-program-to-add-two-complex-numbers'],
1616
pages['multiply-two-numbers'],
17+
pages['java-program-to-check-Leap-year'],
1718
],
1819
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: Java Program to check Leap year
3+
ShortTitle: Check Leap year
4+
description: In this program, you'll learn to check wheather the given Year (integer) is a leap year or not.
5+
---
6+
7+
import { TipInfo } from '@/components/Tip'
8+
9+
To understand this example, you should have the knowledge of the following Java programming topics:
10+
- [Java Operator](/docs/operator)
11+
- [Java Basic Input and Output](/docs/basic-input-output)
12+
13+
## Check Leap year
14+
15+
A java program that checks wheather the given year is leap year or not is as follows:
16+
17+
```java
18+
import java.util.Scanner;
19+
20+
class checkLeapYear{
21+
public static void main(String[] args){
22+
int year;
23+
boolean isLeapYear;
24+
System.out.print("Enter the year to check: ");
25+
// instance of scanner class
26+
Scanner input = new Scanner(System.in);
27+
// taking user input in year variable
28+
year = input.nextInt();
29+
// checking weather the year is leap or not
30+
isLeapYear = year % 4 == 0;
31+
32+
if(isLeapYear){
33+
System.out.println(year + " is leap year");
34+
}else{
35+
System.out.println(year + " isn't leap year");
36+
}
37+
input.close();
38+
}
39+
}
40+
```
41+
42+
#### Output 1
43+
44+
```text
45+
Enter the year to check: 2003
46+
2003 isn't leap year
47+
```
48+
#### Output 2
49+
```text
50+
Enter the year to check: 2004
51+
2004 is leap year
52+
```
53+
54+
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
55+
<TipInfo>
56+
57+
Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)
58+
59+
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).
60+
61+
</TipInfo>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy