We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
interface AdvancedArithmetic{
int divisorSum(int n);
}
class Calculator implements AdvancedArithmetic {
public int divisorSum(int n) {
int sum=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
sum+=i;
}
}
return sum;
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
scan.close();
AdvancedArithmetic myCalculator = new Calculator();
int sum = myCalculator.divisorSum(n);
System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
System.out.println(sum);
}
}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 19: Interfaces
You are viewing a single comment's thread. Return to all comments →
`In Java 8 import java.io.; import java.util.;
interface AdvancedArithmetic{ int divisorSum(int n); } class Calculator implements AdvancedArithmetic { public int divisorSum(int n) { int sum=0; for(int i=1;i<=n;i++) { if(n%i==0) { sum+=i; } } return sum; } }
class Solution {
} `