Given two positive number **N** and **X**. The task is to find the sum of digits of a number formed by N repeating X number of times until sum become single digit.

**Examples :**

Input : N = 24, X = 3 Output : 9 Number formed after repeating 24 three time = 242424 Sum = 2 + 4 + 2 + 4 + 2 + 4 = 18 Sum is not the single digit, so finding the sum of digits of 18, 1 + 8 = 9 Input : N = 4, X = 4 Output : 7

As discussed in this post, recursive sum of digits is 9 if number is multiple of 9, else n % 9. Since divisibility and modular arithmetic are compatible with multiplication, we simply find result for single occurrence, multiply result with x and again find the result.

**How does this work ?**

Lets N = 24 and X = 3.

So, sumUntilSingle(N) = 2 + 4 = 6.

Multiplying 6 by 3 = 18

sumUntilSingle(18) = 9.

Below is the implemenatation of this approach:

## C++

`// C++ program to find Sum of digits of a ` `// number formed by repeating a number X number of ` `// times until sum become single digit. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// return single digit sum of a number. ` `int` `digSum(` `int` `n) ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` `return` `(n % 9 == 0) ? 9 : (n % 9); ` `} ` ` ` `// Returns recursive sum of digits of a number ` `// formed by repeating a number X number of ` `// times until sum become single digit. ` `int` `repeatedNumberSum(` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `sum = x*digSum(n); ` ` ` `return` `digSum(sum); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 24, x = 3; ` ` ` `cout << repeatedNumberSum(n, x) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find Sum of digits of a ` `// number formed by repeating a number X number of ` `// times until sum become single digit. ` ` ` `class` `GFG { ` ` ` ` ` `// return single digit sum of a number. ` ` ` `static` `int` `digSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `0` `; ` ` ` `return` `(n % ` `9` `== ` `0` `) ? ` `9` `: (n % ` `9` `); ` ` ` `} ` ` ` ` ` `// Returns recursive sum of digits of a number ` ` ` `// formed by repeating a number X number of ` ` ` `// times until sum become single digit. ` ` ` `static` `int` `repeatedNumberSum(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` `int` `sum = x * digSum(n); ` ` ` `return` `digSum(sum); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `24` `, x = ` `3` `; ` ` ` `System.out.println(repeatedNumberSum(n, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find Sum of digits of a ` `# number formed by repeating a number X number ` `# of times until sum become single digit. ` ` ` `# Return single digit sum of a number ` `def` `digSum(n): ` ` ` `if` `n ` `=` `=` `0` `: ` ` ` `return` `0` ` ` `return` `(n ` `%` `9` `=` `=` `0` `) ` `and` `9` `or` `(n ` `%` `9` `) ` ` ` `# Returns recursive sum of digits of a number ` `# formed by repeating a number X number of ` `# times until sum become single digit. ` `def` `repeatedNumberSum(n, x): ` ` ` `sum` `=` `x ` `*` `digSum(n) ` ` ` `return` `digSum(` `sum` `) ` ` ` `# Driver Code ` `n ` `=` `24` `; x ` `=` `3` `print` `(repeatedNumberSum(n, x)) ` ` ` `# This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find Sum of digits of a ` `// number formed by repeating a number X ` `// number of times until sum becomes ` `// single digit. ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// return single digit sum of a number. ` ` ` `static` `int` `digSum(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 0) ` ` ` `return` `0; ` ` ` ` ` `return` `(n % 9 == 0) ? 9 : (n % 9); ` ` ` `} ` ` ` ` ` `// Returns recursive sum of digits of a ` ` ` `// number formed by repeating a number X ` ` ` `// number of times until sum become ` ` ` `// single digit. ` ` ` `static` `int` `repeatedNumberSum(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` `int` `sum = x * digSum(n); ` ` ` `return` `digSum(sum); ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 24, x = 3; ` ` ` `Console.Write( repeatedNumberSum(n, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find Sum ` `// of digits of a number ` `// formed by repeating a number ` `// X number of times until ` `// sum becomes single digit. ` ` ` `// return single digit ` `// sum of a number. ` `function` `digSum(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 0) ` ` ` `return` `0; ` ` ` `return` `(` `$n` `% 9 == 0) ? 9 : (` `$n` `% 9); ` `} ` ` ` `// Returns recursive sum of ` `// digits of a number formed ` `// by repeating a number X ` `// number of times until sum ` `// become single digit. ` `function` `repeatedNumberSum( ` `$n` `, ` `$x` `) ` `{ ` ` ` `$sum` `= ` `$x` `* digSum(` `$n` `); ` ` ` `return` `digSum(` `$sum` `); ` `} ` ` ` `// Driver Code ` `$n` `= 24; ` `$x` `= 3; ` `echo` `repeatedNumberSum(` `$n` `, ` `$x` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

9

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find M-th number whose repeated sum of digits of a number is N
- Number formed after K times repeated addition of smallest divisor of N
- Check if a number is magic (Recursive sum of digits is 1)
- Recursive sum of digits of a number is prime or not
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count ways to spell a number with repeated digits
- Remove repeated digits in a given number
- Minimum digits to be removed to make either all digits or alternating digits same
- Smallest number with given sum of digits and sum of square of digits
- Total numbers with no repeated digits in a range
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Find maximum number that can be formed using digits of a given number
- Find smallest number formed by inverting digits of given number N
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Minimum sum of two numbers formed from digits of an array
- Sum of all numbers that can be formed with permutations of n digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.