Algorithm
Problem Name: 30 days of code -
Objective
Today, we're learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!
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 20 are 1,2,4,5,10,20 and their sum is 42.
Input Format
A single line with an integer, n.
Constraints
- n <= 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
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
class AdvancedArithmetic {
public:
virtual int divisorSum(int n)=0;
};
class Calculator : public AdvancedArithmetic {
public:
int divisorSum(int n) {
int sum = 0;
for (int i = 1; i < = n; i++)
if (n % i == 0) sum += i;
return sum;
}
};
int main() {
int n;
cin >> n;
AdvancedArithmetic *myCalculator = new Calculator();
int sum = myCalculator->divisorSum(n);
cout << "I implemented: AdvancedArithmetic\n" << sum;
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C# Programming
Code -
C# Programming
using System;
public interface AdvancedArithmetic{
int divisorSum(int n);
}
public class Calculator : 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{
static void Main(string[] args){
int n = Int32.Parse(Console.ReadLine());
AdvancedArithmetic myCalculator = new Calculator();
int sum = myCalculator.divisorSum(n);
Console.WriteLine("I implemented: AdvancedArithmetic\n" + sum);
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
interface AdvancedArithmetic {
int divisorSum(int n);
}
class Calculator implements AdvancedArithmetic {
@Override
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);
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
Python Programming
class AdvancedArithmetic(object):
def divisorSum(n):
raise NotImplementedError
class Calculator(AdvancedArithmetic):
def divisorSum(self, n):
s = 0
for i in range(1,n+1):
if (n%i == 0):
s+=i
return s
n = int(input())
my_calculator = Calculator()
s = my_calculator.divisorSum(n)
print("I implemented: " + type(my_calculator).__bases__[0].__name__)
print(s)
Copy The Code &
Try With Live Editor
#5 Code Example with PHP Programming
Code -
PHP Programming
divisorSum($n);
echo "I implemented: AdvancedArithmetic\n".$sum;
}
else
{
echo "Wrong answer";// You will get this output if you dont implement
}
?>
Copy The Code &
Try With Live Editor