Coders Packet

Java Program to Recursive Digit Sum

By CHATTETI.THARUN

In this article, we will be solving a problem in which we need to find the Super Digit of integer p (obtained by concatenating n, k times )and return its value.

Problem Statement:

Given an integer, we need to find the superDigit of the integer.

superDigit is defined as :

If X has only one digit, then its super digit is 1.
Otherwise, the super digit of X is equal to the super digit of the sum of the digits of X.

for example, the superDigit of 9875 will be calculated as: 

        super_digit(9875)   	9+8+7+5 = 29 
	super_digit(29) 	2 + 9 = 11
	super_digit(11)		1 + 1 = 2
	super_digit(2)		= 2  

Function Description:

Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.

superDigit has the following parameter(s):

  • string n: a string representation of an integer
  • int k: the times to concatenate n to make p

Example:

n=9875

k=4

The number n is created by concatenating the string n, k times so the initial p = 9875987598759875.

    superDigit(p) = superDigit(9875987598759875)
                     9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116
    superDigit(p) = superDigit(116)
                     1+1+6 = 8
    superDigit(p) = superDigit(8)

I strongly recommend you to try it yourself before looking into the solution.

INPUT FORMAT:
The first line contains two space separated integers, n and k.

Solution:

Here you need to take a number 'n' as Input and then concatenate it to ' k ' times and after concatenation, the final result will be stored as p.
now, we need to calculate superDigit of p. superDigit of a number is adding of digits of a number until a single digit is obtained.

first, we need to calculate the sum of digits of a number. for that, we need a function that computes the sum of digits of a number.

 

private static int fun(long p) 
    {
        if (p<10)
        return (int) p;    
        return fun(fun(p/10)+p%10);    
    }

 

the above function calculates and returns the sum of digits of a number up to a single digit.

now, we need to take a string n and concatenate it k times and convert it into integer and assign it to  ' p '.

 

CODE:

 

import java.io.*;
 class Result 
 {
    public static int superDigit(String n, long k) {
     long num = 0; 
        for(int i=0; i<n.length(); i++) 
        {
            num += Integer.parseInt(n.charAt(i)+"");
        } 
        num =  fun(num*k);
        int p = (int) num;
        return fun(p);}

    private static int fun(long p) 
    {
        if (p<10)
        return (int) p;    
        return fun(fun(p/10)+p%10);    
    }

 }

 public class Solution
 {
    public static void main(String[] args) throws IOException 
    {
        Scanner s=new Scanner(System.in);  
        String[] firstMultipleInput =s.nextLine().replaceAll("\\s+$", "").split(" ");
        String n = firstMultipleInput[0];
        int k = Integer.parseInt(firstMultipleInput[1]);
        int result = Result.superDigit(n, k);
        System.out.println((String.valueOf(result)));
        s.close();
    } 

 }

 you can try to solve this problem here.

 

Download project

Reviews Report

Submitted by CHATTETI.THARUN (Tharun)

Download packets of source code on Coders Packet