Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 19 Interfaces 30 days of code solution

YASH PAL, 31 July 202413 October 2025

Hackerrank Day 19 Interfaces problem solution – In this problem set, we need to take an integer input and then we need to subtract that integer and then add all the numbers and print the sum of them on the output screen.

Objective
Today, we’re learning about Interfaces.

Task
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 25 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.

Problem solution in Python 2 programming.

class Calculator(AdvancedArithmetic):
    def divisorSum(self, n):
        return divisors(n)
    
    
def divisors(n):
    c = 1
    s = 0
    while c <= n:
        if n % c == 0:
            s += c
        c += 1
    return s

Problem solution in Python 3 programming.

class Calculator(AdvancedArithmetic):
    def divisorSum(self, n):
        temp = []
        for i in range(1, n+1):
            if n%i == 0:
                temp.append(i)
        return sum(temp)

Problem solution in java programming.

class Calculator implements AdvancedArithmetic{
    public int divisorSum(int n){
        // n has no divisors other than itself
        if(n == 1){
            return n;
        }
        
        // Find and sum divisors:
        int half = n/2;
        int sum = n;
        do{
            if(n % half == 0){
                sum += half;
            }
        }while( half-- > 1 );
        
        return sum;
    }
}

Problem solution in c++ programming.

class Calculator : public AdvancedArithmetic {
    
    int divisorSum(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
                sum += i;
            }
        }
        return sum;
    }
    
};
30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes