Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • 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
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Recursive Digit Sum problem solution

YASH PAL, 31 July 2024

In this HackerRank Recursive Digit Sum Interview preparation kit problem you need to Complete the function superDigit that must return the calculated super digit as an integer.

HackerRank Recursive Digit Sum Interview preparation kit solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

def sup_digits(x,k):
    a = digsum(x)
    return sup_digit(str(int(a)*k))

def sup_digit(x):
    if len(x) <= 1:
        return x
    else:
        return sup_digit( digsum(x) )

def digsum(x):
    return str(sum((int(i) for i in list(x))))


n, k = input().split()
print( sup_digits(n, int(k)))

Problem solution in Java Programming.

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

public class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        Scanner sc = new Scanner(System.in);
        
        String str_n = sc.next();
        int k = sc.nextInt();
        
        int pSum = Integer.parseInt(s.supdig(str_n));
        pSum *= k;
                        
        String sup = Integer.toString(s.supdig(pSum));
        
        System.out.println(sup);
    }
    
    String supdig(String n) {
        if(n.length() == 1) return n;
        else {
            int np = 0;
            
            for(int i = 0; i < n.length(); i++) {
                np += Character.getNumericValue( n.charAt(i) );    
            }
            
            return supdig(Integer.toString(np));
        }       
    }
    
    int supdig(int n) {
        if(n / 10 == 0) return n;
        else {
            int r = 0;
            
            while(n > 0) {
                r += n % 10;
                n /= 10;
            }
            
            return supdig(r);
        }
    }
}

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int super_digit( long long k ) {
    if ( k < 10 ) {
        return k;
    }
    
    long long sum = 0;
    while( k > 0 ) {
        sum += k % 10;
        k = k / 10;
    }
    return super_digit( sum );
}


long long sum_initial( string number ){
    long long sum = 0;
    
    for( int i = 0; i < number.size(); i++ ) {
        sum += number[i] - '0';
    }
    
    return sum;
}


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    string  n;
    int k;
    cin >> n >> k;
    long long repeated = sum_initial(n) * k;
    
    long long result = super_digit(repeated);
    
    cout << result << "n";
    
    
    
    
    return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    long long sd=0,h;
    int k;
    int c;
    do{
        c=getchar();
        if(c != ' ')
            sd += c -'0'; 
    }while(c != ' ');
    scanf("%d",&k);
    sd *= k;
    
    while(sd > 10){
        h=0;
        while(sd > 0){
            h+= sd %10;
            sd = sd /10;
        }
        sd =h;
    }
    printf("%lldn",sd);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Problem solution in JavaScript programming.

const N = require('bignumber.js');

let $ = '';

const r = n => n < 10 ? n : f(new N(n).toFixed());

const f = (n, k = 1) => {
  let i = 0;

  for (const c of n) i += +c;

  return r(i * k);
};

process.stdin.on('data', d => $ += d).on('end', () => {
  const [ n, k ] = $.trim().split(/s+/);
  const ans = f(new N(n).toFixed(), +k);

  console.log(ans);
});

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes