Table of Contents
Let’s look at various input and output scenarios for this
The method used in the following program is as follows
Example
Output
Home Backend Development C++ Recursively sum the sum of the digits of a number formed by repeated additions, implemented in C++

Recursively sum the sum of the digits of a number formed by repeated additions, implemented in C++

Sep 02, 2023 pm 02:01 PM
recursion c Sum

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;
}
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recursive implementation of C++ functions: Is there a limit to recursion depth? Recursive implementation of C++ functions: Is there a limit to recursion depth? Apr 23, 2024 am 09:30 AM

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.

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

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.

VSCode and VS C++ IntelliSense not working or picking up libraries VSCode and VS C++ IntelliSense not working or picking up libraries Feb 29, 2024 pm 01:28 PM

VS Code and Visual Studio C++ IntelliSense may not be able to pick up libraries, especially when working on large projects. When we hover over #Include&lt;wx/wx.h&gt;, we see the error message "CannotOpen source file 'string.h'" (depends on "wx/wx.h") and sometimes, autocomplete Function is unresponsive. In this article we will see what you can do if VSCode and VSC++ IntelliSense are not working or extracting libraries. Why doesn't my Intellisense work in C++? When working with large files, IntelliSense sometimes

Do you know how to sum a Word table? Do you know how to sum a Word table? Mar 21, 2024 pm 01:10 PM

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

Fix Xbox error code 8C230002 Fix Xbox error code 8C230002 Feb 27, 2024 pm 03:55 PM

Are you unable to purchase or watch content on your Xbox due to error code 8C230002? Some users keep getting this error when trying to purchase or watch content on their console. Sorry, there's a problem with the Xbox service. Try again later. For help with this issue, visit www.xbox.com/errorhelp. Status Code: 8C230002 This error code is usually caused by temporary server or network problems. However, there may be other reasons, such as your account's privacy settings or parental controls, that may prevent you from purchasing or viewing specific content. Fix Xbox Error Code 8C230002 If you receive error code 8C when trying to watch or purchase content on your Xbox console

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

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.

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

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.

A beginner's guide to C++ recursion: Building foundations and developing intuition A beginner's guide to C++ recursion: Building foundations and developing intuition May 01, 2024 pm 05:36 PM

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.

See all articles