


Recursively sum the sum of the digits of a number formed by repeated additions, implemented in C++
Given two integers "number" and "repeat" as input. The goal is to calculate the numerical sum of an input number repeated a "repeat" number of times until the sum becomes a single number. Continue in this way until the sum of the numbers obtained becomes a single digit. If the input number is 123 and repeat=2 then the sum of 123123 will be 1 2 3 1 2 3=12 This is not a single digit. Now the sum of the digits of 12 is 1 2 = 3. The output will be 3
Let’s look at various input and output scenarios for this
Input− number=32 Repeat=3
Output− The recursive sum of the numbers formed by repeated appending is: 6
Explanation − The sum of the numbers of 323232 is 3 2 3 2 3 2=15, and the sum of the digits of 15 is 1 5=6. 6 is a single digit, so the output will be 6.
Input− number=81 Repeat=4
Output−The recursive digital sum of the numbers formed by repeated appending is: 9
Explanation - The sum of the digits of 81818181 is 1 8 1 8 1 8 1 8=36, and the sum of the digits of 36 is 3 6=9. 9 is a single digit, so the output will be 9.
The method used in the following program is as follows
Declare two integer type variables such as number and repeat. Pass the data to the function as Recursive_Sum(number, Repeat).
-
Inside the function as Recursive_Sum(int number, int Repeat)
Declare an integer variable as total and use repeat * sum(number ) sets it;
Returns a call to the function as sum(total) .
-
Inside the function as sum (int number)
Check if the IF number is 0 and return 0.
Checks if the IF number % 9 is 0 and returns 9.
ELSE, return number % 9
Print the result.
Example
#include <bits/stdc++.h> using namespace std; int sum(int number){ if(number == 0){ return 0; } if(number % 9 == 0){ return 9; } else{ return number % 9; } } int Recursive_Sum(int number, int repeat){ int total = repeat * sum(number); return sum(total); } int main(){ int number = 12; int repeat = 4; cout<<"Recursive sum of digits of a number formed by repeated appends is: "<<Recursive_Sum(number, repeat); return 0; }
Output
If we run the above code it will generate the following output
Recursive sum of digits of a number formed by repeated appends is: 3
The above is the detailed content of Recursively sum the sum of the digits of a number formed by repeated additions, implemented in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

Sometimes, we often encounter counting problems in Word tables. Generally, when encountering such problems, most students will copy the Word table to Excel for calculation; some students will silently pick up the calculator. Calculate. Is there a quick way to calculate it? Of course there is, in fact the sum can also be calculated in Word. So, do you know how to do it? Today, let’s take a look together! Without further ado, friends in need should quickly collect it! Step details: 1. First, we open the Word software on the computer and open the document that needs to be processed. (As shown in the picture) 2. Next, we position the cursor on the cell where the summed value is located (as shown in the picture); then, we click [Menu Bar

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

Recursive definition and optimization: Recursive: A function calls itself internally to solve difficult problems that can be decomposed into smaller sub-problems. Tail recursion: The function performs all calculations before making a recursive call, which can be optimized into a loop. Tail recursion optimization condition: recursive call is the last operation. The recursive call parameters are the same as the original call parameters. Practical example: Calculate factorial: The auxiliary function factorial_helper implements tail recursion optimization, eliminates the call stack, and improves efficiency. Calculate Fibonacci numbers: The tail recursive function fibonacci_helper uses optimization to efficiently calculate Fibonacci numbers.

Recursion is a powerful technique that allows a function to call itself to solve a problem. In C++, a recursive function consists of two key elements: the base case (which determines when the recursion stops) and the recursive call (which breaks the problem into smaller sub-problems ). By understanding the basics and practicing practical examples such as factorial calculations, Fibonacci sequences, and binary tree traversals, you can build your recursive intuition and use it in your code with confidence.
