# Day 19: Interfaces | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 19: Interfaces HackerRank Solution in C++, Java and Python.

## Objective

The `AdvancedArithmetic` interface and the method declaration for the abstract `divisorSum(n)` method are provided for you in the editor below.

Complete the implementation of `Calculator` class, which implements the `AdvancedArithmetic` interface. The implementation for the `divisorSum(n)` method must return the sum of all divisors of n.

Example
n = 25
The divisors of 25 are 1, 5, 25. Their sum is 31.
n = 20
The divisors of 20 are 1 , 2, 4, 5, 10, 20 and their sum is 42.

## Input Format

A single line with an integer, n.

## Constraints

• 1 <= n <= 1000

## Output Format

You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.

Sample Input

``6``

Sample Output

``````I implemented: AdvancedArithmetic
12``````

Explanation

The integer 6 is evenly divisible by 123, and 6. Our divisorSum method should return the sum of these numbers, which is 1 + 2 + 3 + 6 = 12. The Solution class then prints I implemented: AdvancedArithmetic on the first line, followed by the sum returned by divisorSum (which is 12) on the second line.

## Solution – Day 19: Interfaces

### C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
public:
virtual int divisorSum(int n)=0;
};

class Calculator : public AdvancedArithmetic {
public:
int divisorSum(int n) {
int s=0,i=1;
while(i<=n){
if(n%i==0){
s=s+i;
// n=n/i;
i++;
}
else {
i++;
}
}
return s;
}
};
int main(){
int n;
cin >> n;
int sum = myCalculator->divisorSum(n);
cout << "I implemented: AdvancedArithmetic\n" << sum;
return 0;
}```

### Java

```import java.io.*;
import java.util.*;

int divisorSum(int n);
}
public int divisorSum(int n) {
int sum=0;
for(int i=1;i<=n;i++)
if(n%i==0)
sum=sum+i;
return sum;
}
}
class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();

int sum = myCalculator.divisorSum(n);
System.out.println("I implemented: " + myCalculator.getClass().getInterfaces().getName() );
System.out.println(sum);
}
}```

### Python

```class AdvancedArithmetic(object):
def divisorSum(n):
raise NotImplementedError

def divisorSum(self, n):
val = 0
for i in range(1,n+1):
if(n % i == 0):
val += i
return val

n = int(input())
my_calculator = Calculator()
s = my_calculator.divisorSum(n)
print("I implemented: " + type(my_calculator).__bases__.__name__)
print(s)```

Disclaimer: The above Problem (Day 19: Interfaces) is generated by Hacker Rank but the Solution in Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.