-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.java
More file actions
49 lines (34 loc) · 857 Bytes
/
Copy pathInterface.java
File metadata and controls
49 lines (34 loc) · 857 Bytes
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
48
49
/*
Question:
Implement the interface AdvancedArithmetic.
Create a class MyCalculator that implements
the method divisor_sum(int n).
The method should return the sum of all divisors of n.
Example:
Input: 6
Divisors: 1, 2, 3, 6
Output: 12
*/
import java.util.*;
// Interface
interface AdvancedArithmetic {
// Method signature
int divisor_sum(int n);
}
// Class implementing the interface
class MyCalculator implements AdvancedArithmetic {
// Implement divisor_sum method
public int divisor_sum(int n) {
int sum = 0;
// Check all numbers from 1 to n
for (int i = 1; i <= n; i++) {
// If i is a divisor of n
if (n % i == 0) {
// Add divisor to sum
sum += i;
}
}
// Return total sum of divisors
return sum;
}
}