Skip to content

Commit a22571d

Browse files
committed
Largest Divisor
1 parent 7fe567b commit a22571d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.java.numbers;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* Largest Divisor
7+
*
8+
* Write a program to find the Largest Divisor of a number
9+
* Largest divisor except the given number.
10+
*
11+
* Say the number is 100
12+
* Divisors are : 1, 2, 4, 5, 10, 20, 25, 50
13+
* Largest Divisor is 50
14+
*
15+
* Say the number is 25
16+
* Divisors are : 1, 5
17+
* Largest Divisor is 5
18+
*
19+
*/
20+
public class LargestDivisor {
21+
public static void main(String[] args) {
22+
Scanner scanner = new Scanner(System.in);
23+
System.out.println("Enter a number :: ");
24+
int n = scanner.nextInt();
25+
int output = largestDivisor(n);
26+
System.out.println("Largest Divisor is : "+output);
27+
scanner.close();
28+
}
29+
30+
public static int largestDivisor(int n){
31+
for(int i=2;i<=Math.sqrt(n);i++)
32+
if(n%i == 0)
33+
return n/i;
34+
return 1;
35+
}
36+
}
37+
38+
/*
39+
Enter a number :: 100
40+
Largest Divisor is : 50
41+
42+
Enter a number :: 23
43+
Largest Divisor is : 1
44+
45+
Enter a number :: 25
46+
Largest Divisor is : 5
47+
*/

0 commit comments

Comments
 (0)