Skip to content
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 @@ -16,5 +16,6 @@ export const programsNav = {
pages['multiply-two-numbers'],
pages['java-program-to-check-Leap-year'],
pages['calculate-simple-interest'],
pages['java-program-to-check-divisbility'],
],
}
162 changes: 162 additions & 0 deletions src/pages/programs/java-program-to-check-divisbility.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
---
title: Java Program to Check Divisibility
ShortTitle: Divisibility Checker
description: This Java program checks the divisibility of a number by 2, 3, 5, 7, or 11.
---


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 Divisbilty

A java program that checks wheather the User Given Number is Divisble by 2,3,5,7,11 is as follows


```java
import java.util.Scanner;

public class DivisibilityChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");
int number = scanner.nextInt();

System.out.println("Choose divisibility checks:");
System.out.println("1. Check divisibility by 2");
System.out.println("2. Check divisibility by 3");
System.out.println("3. Check divisibility by 5");
System.out.println("4. Check divisibility by 7");
System.out.println("5. Check divisibility by 11");
System.out.println("Enter option (1-5): ");

int option = scanner.nextInt();

switch (option) {
case 1:
if (number % 2 == 0) {
System.out.println(number + " is divisible by 2.");
} else {
System.out.println(number + " is not divisible by 2.");
}
break;
case 2:
if (number % 3 == 0) {
System.out.println(number + " is divisible by 3.");
} else {
System.out.println(number + " is not divisible by 3.");
}
break;
case 3:
if (number % 5 == 0) {
System.out.println(number + " is divisible by 5.");
} else {
System.out.println(number + " is not divisible by 5.");
}
break;
case 4:
if (number % 7 == 0) {
System.out.println(number + " is divisible by 7.");
} else {
System.out.println(number + " is not divisible by 7.");
}
break;
case 5:
if (number % 11 == 0) {
System.out.println(number + " is divisible by 11.");
} else {
System.out.println(number + " is not divisible by 11.");
}
break;
default:
System.out.println("Invalid option. Please choose a number between 1 and 5.");
}

scanner.close();
}
}

```
#### Output 1

```Checking the divisbilty by 2
Enter a number: 14
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 1
14 is divisible by 2.
```
#### Output 2
```Checking the divisbilty by 3
Enter a number: 27
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 2
27 is divisible by 3.

```
#### Output 3
```Checking the divisbilty by 5
Enter a number: 45
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 3
45 is divisible by 5.


```
#### Output 4
```Checking the divisbilty by 11
Enter a number: 33
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 5
33 is divisible by 11.



```
#### Output 5
```Invalid Option
Enter a number: 18
Choose divisibility checks:
1. Check divisibility by 2
2. Check divisibility by 3
3. Check divisibility by 5
4. Check divisibility by 7
5. Check divisibility by 11
Enter option (1-5): 6
Invalid option. Please choose a number between 1 and 5.




```
<TipInfo>

Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)



</TipInfo>