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