Today we are going to solve HackerRank Day 19 : Interfaces 30 days of code solution 30 days of code solution in C, C++, Java, Python & Javascript.
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 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 1, 2, 3, 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.
HackerRank Day 19 : Interfaces 30 days of code solution
Interfaces HackerRank Solution in C++
//Write your code here //CODINGWITHNICK 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; } };
Interfaces HackerRank Solution in Java
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; } }
Interfaces HackerRank Solution in Python 3
#Write your code here #CODINGWITHNICK 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)
NEXT : HackerRank Day 20 : Sorting 30 days of code solution
30 Days of Code HackerRank Solutions List – Day 0 to Day 29
Read More –
- How To Create Responsive Image Gallery Using Html & Css
- Create a Quiz App using HTML CSS & JavaScript
- Product Landing Page Using Html Css Bootstrap
- Home Page With Image Gallery Website Using Html Css Js Bootstrap4
- How create an Complete Responsive Ecommerce Website Using Html Css Js & Bootstrap