Super digit sum

in this problem we have to calculate sum of digit until it comes in single digit for example
if 9875 is input
first time sum of digit will be 9+8+7+5=29
29 have 2 digit so we will again count…
2+9=11
11 have also 2 digit
so 1+1=2 will be final answer …
here for input 123 my output is printing only once but for 9875 my output is printing 4 times___I want to print only once…plz help

package Hackerrank;

import java.util.Scanner;

public class Recursive_digit_sum {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	long num = sc.nextLong();
	int sum=0;
	superDigit(num,sum);
	//System.out.println(ans);
}

public static void superDigit(long num,int sum) {
	if(num==0 && sum>=10) {
		num=sum;
		sum=0;
        superDigit(num, sum);
	}
	if(num==0 && sum<10) {
		System.out.println(sum);
		return;
	}
	
   sum=sum+(int)(num%10);	
   superDigit(num/10, sum);
}

}

Hey @shivamrai0307

add return; after this otherwise your function will keep on going even after the call

1 Like

@aa1 thank you sir got it!

1 Like