The Wayback Machine - https://web.archive.org/web/20240901093239/https://www.geeksforgeeks.org/recursive-sum-digits-number-formed-repeated-appends/
Open In App

Recursive sum of digits of a number formed by repeated appends

Last Updated : 25 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
64.03%
6.6K

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 implementation 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;
}


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.


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.


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


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.
?>


Javascript




<script>
 
// Javascript 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
let n = 24;
let x = 3;
 
document.write(repeatedNumberSum(n, x));
 
// This code is contributed by _saurabh_jaiswal.
 
</script>


Output : 

9

 



Similar Reads

Minimum number of Appends of X or Y characters from the end to the front required to obtain given string
Given a string S and two positive integers X and Y, the task is to find the minimum number of operations required to obtain the original string. In each operation, append X or Y characters from the end of the string to the front of the string respectively in each operation. Examples: Input: S = "GeeksforGeeks", X = 5, Y = 3Output: 3Explanation:Belo
9 min read
Minimum number of Appends needed to make a string palindrome
Given a string s we need to tell minimum characters to be appended (insertion at the end) to make a string palindrome. Examples: Input : s = "abede"Output : 2We can make string palindrome as "abedeba"by adding ba at the end of the string.Input : s = "aabb"Output : 2We can make string palindrome as"aabbaa"by adding aa at the end of the string. The s
13 min read
Number formed by deleting digits such that sum of the digits becomes even and the number odd
Given a non-negative number N, the task is to convert the number by deleting some digits of the number, such that the sum of the digits becomes even but the number is odd. In case there is no possible number, then print -1.Note: There can be multiple numbers possible for a given N.Examples: Input: N = 18720 Output: 17 Explanation: After Deleting 8,
5 min read
Maximum possible time that can be formed from four digits | (Recursive Approach)
Given an array arr[] with 4 integers. The task is to return the maximum 24 hour time that can be formed using the digits from the array. Note that the minimum time in 24-hour format is 00:00, and the maximum is 23:59. If a valid time cannot be formed then return an empty string.Examples: Input: arr[] = {1, 2, 3, 4} Output: 23:41Explanation: 23:41 i
12 min read
Find M-th number whose repeated sum of digits of a number is N
Given two positive integers N and M, The task is to find the M-th number whose sum of digits of a number until the sum becomes a single digit is N. Examples: Input: N = 1, M = 3Output: 19 The first two numbers are 1 and 10. Input: N = 2, M = 5Output: 38 The first four numbers are 2, 11, 20, and 29. A naive approach is to iterate for all numbers and
8 min read
Check if a number is magic (Recursive sum of digits is 1)
A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number. For example- Number= 50113 =&gt; 5+0+1+1+3=10 =&gt; 1+0=1 This is a Magic Number For example- Number= 1234 =&gt;
7 min read
Recursive sum of digits of a number is prime or not
Given a number n, we need to find the sum of each digits of the number till the number becomes a single digit. We need to print "yes" if the sum is a prime or "no" if it is not prime. Examples: Input : 5602 Output: No Explanation: Step 1- 5+6+0+2 = 13 Step 2- 1+3 = 4 4 is not prime Input : 56 Output : Yes Explanation: Step 1- 5+6 = 11 Step 2- 1+1 =
5 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Number formed after K times repeated addition of smallest divisor of N
Given two integers N and K, the task is to generate the final outcome of performing K operations which involves adding the smallest divisor, other than 1, of the current value of N to it at every step.Example: Input: N = 9, K = 4 Output: 18 Explanation: Divisors of 9 are {1, 3, 9} 1st Operation: N = 9 + 3 = 12 2nd Operation: N = 12 + 2 = 14 3rd Ope
5 min read
Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
Given two integers N and K, the task is to count the numbers up to N digits such that no two zeros are adjacents and the range of digits are from 0 to K-1.Examples: Input: N = 2, K = 3 Output: 8 Explanation: There are 8 such numbers such that digits are from 0 to 2 only, without any adjacent 0s: {1, 2, 10, 11, 12, 20, 21, 22}Input: N = 3, K = 3 Out
12 min read
Smallest number with given sum of digits and sum of square of digits
Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input : a = 18, b = 162 Output : 99 Explanation : 99 is t
15+ min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Find second smallest number from sum of digits and number of digits
Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Explanation: 169 is the smallest number possible with s
8 min read
Find smallest number with given number of digits and sum of digits
How to find the smallest number with given digit sum s and number of digits d? Examples : Input : s = 9, d = 2 Output : 18 There are many other possible numbers like 45, 54, 90, etc with sum of digits as 9 and number of digits as 2. The smallest of them is 18. Input : s = 20, d = 3 Output : 299 Recommended PracticeSmallest numberTry It! A Simple So
9 min read
Find the Largest number with given number of digits and sum of digits
Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max variable to store the maximum number with m digits
13 min read
Remove repeated digits in a given number
Given an integer, remove consecutive repeated digits from it. Examples: Input: x = 12224 Output: 124 Input: x = 124422 Output: 1242 Input: x = 11332 Output: 132 We need to process all digits of n and remove consecutive representations. We can go through all digits by repeatedly dividing n with 10 and taking n%10. C/C++ Code // C++ program to remove
5 min read
Count ways to spell a number with repeated digits
Given a string that contains digits of a number. The number may contain many same continuous digits in it. The task is to count number of ways to spell the number. For example, consider 8884441100, one can spell it simply as triple eight triple four double two and double zero. One can also spell as double eight, eight, four, double four, two, two,
6 min read
Maximize the number formed by replacing adjacent digits with their sum
Given a number N without leading 0s, the task is to find the maximum number formed after replacing any two adjacent digits by their sum only once. Examples: Input: 10057 Output: 10012 Explanation: 10012 is the maximum number that we can form since 5+7=12. Input: 30 Output: 3 .Explanation: 3+0=3 the only option . Approach: The problem can be solved
9 min read
Minimize the number formed by replacing adjacent pair of digits with their sum
Given string s denoting a number. The task is to find the minimum number that can be formed after replacing two consecutive digits of s by their sum. Examples: Input: s = "1005"Output: 105Explanation: Select and replace any two consecutive digits with its sum Input: s = "56773"Output: 11773Explanation: Select and replace first two consecutive digit
7 min read
Count primes less than number formed by replacing digits of Array sum with prime count till the digit
Given an array arr[] of size N having only positive elements, the task is to find the number of primes less than the number formed after following the below operations: Add all elements of the given array, say sumReplace every digit of the sum with the total number of prime numbers occurring between 0 and that digit. Examples: Input: N = 5, arr[] =
7 min read
Check if string S1 can be formed using repeated insertions of another string S2
Given two strings S1 and S2 consisting of unique characters, the task is to check S1 can be formed by repeated insertions of string S2. Input: S1 = "aabb", S2 = "ab"Output: YesExplanation: the mentioned string can be obtained after series of moves: Insert string "ab" in an empty string. Current string will be "ab"insert "ab" after "a". The final st
7 min read
Check if given string is a substring of string formed by repeated concatenation of z to a
Given a string str, the task is to check if string str is a substring of an infinite length string S in which lowercase alphabets are concatenated in reverse order as: S = "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlkjihgfedcba...." Examples: Input: str = "cbaz"Output: YES Explanation:Given string "cbaz" is a valid sub string of S. Input: str = "ywxt
10 min read
Minimum digits to be removed to make either all digits or alternating digits same
Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions: All the elements of the string are the same.All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equ
7 min read
Find smallest number formed by inverting digits of given number N
Given an integer N, the task is to form a minimum possible positive number (&gt;0) by inverting some digits of N. Inverting for a digit T is defined as subtracting it from 9 that is 9 - T. Note: The final number should not start from zero. Examples: Input:N = 4545Output: 4444Explanation:The minimum possible number is 4444 by subtracting the two 5 (
9 min read
Find maximum number that can be formed using digits of a given number
Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.Examples: Input : 38293367 Output : 98763332 Input : 1203465 Output: 6543210 Recommended PracticeFind maximum numberTry It! Simple Approach: The simple method to solve this problem is to extract and store the digits of the given number
6 min read
Check if sum of digits in the left half is divisible by sum of digits in the right half in the largest permutation of N
Given a positive integer N, the task is to maximize the integer N by rearranging the digits and check if the sum of the left half digits is divisible by the sum of the right half digits or not. If found to be true, then print "Yes". Otherwise, print "No". If the number of digits(say D) in the given number N is odd, then consider any of the two poss
8 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Count numbers in given range such that sum of even digits is greater than sum of odd digits
Given two integers L and R denoting a range [L, R]. The task is to find the total count of numbers in the given range [L,R] whose sum of even digits is greater than the sum of odd digits. Examples: Input : L=2 R=10 Output : 4 Numbers having the property that sum of even digits is greater than sum of odd digits are: 2, 4, 6, 8 Input : L=2 R=17 Outpu
14 min read
Count of numbers in range [L, R] having sum of digits of its square equal to square of sum of digits
Given two integers L and R, the task is to find the count of numbers in range [L, R] such that the sum of digits of its square is equal to the square of sum of its digits, Example: Input: L = 22, R = 22Output: 1Explanation: 22 is only valid number in this range as sum of digits of its square = S(22*22) = S(484) = 16 and square of sum of its digits
11 min read
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.Examples: Input: L = 1, R = 100, N = 8, M = 2 Output: 4 Only 8, 26, 44 and 62 are valid numbers Input: L = 1, R = 200, N = 4, M = 11 Output: 2 Only 2
14 min read