Using recursion functions only to calculate the final sum of a given number
so the question is:
**cant use loops in the recursive functions, only for input check
Write a program that uses a recursive function (or function) that receives a positive integer from the user and calculates the "final amount" of his digits.
The sum of the final digits of a number is the result of a process in which calculates the sum of digits of the number and if the sum is not a single digit number and return the sum the digits of the sum until you get a single digit number.
example :
The sum of the digits of 96437 is 29 9 + 6 + 4 + 3 + 7 = 29 And the sum of the digits of 29 is 11 2 + 9 = 11 And the sum of the digits of 11 is 2 1 + 1 = 2
I figured how to use recursion to calculate sum of a number but dont know how to put the right condition to do it so it will be single digit number.
Student for bioinformatics, tried to use if condition in main but cant think of something good.
#include <iostream>
using namespace std;
// recursive function to find sum of digits of a number
int sum(int x)
{
if (x == 0)
{
return 1;
}
return (x % 10 + sum(x / 10));
}
int main()
{
int n, result;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
result = sum(n);
if (result % 10 == 0)
{
cout << result << endl;
}
else
{
}
}
c++
add a comment |
so the question is:
**cant use loops in the recursive functions, only for input check
Write a program that uses a recursive function (or function) that receives a positive integer from the user and calculates the "final amount" of his digits.
The sum of the final digits of a number is the result of a process in which calculates the sum of digits of the number and if the sum is not a single digit number and return the sum the digits of the sum until you get a single digit number.
example :
The sum of the digits of 96437 is 29 9 + 6 + 4 + 3 + 7 = 29 And the sum of the digits of 29 is 11 2 + 9 = 11 And the sum of the digits of 11 is 2 1 + 1 = 2
I figured how to use recursion to calculate sum of a number but dont know how to put the right condition to do it so it will be single digit number.
Student for bioinformatics, tried to use if condition in main but cant think of something good.
#include <iostream>
using namespace std;
// recursive function to find sum of digits of a number
int sum(int x)
{
if (x == 0)
{
return 1;
}
return (x % 10 + sum(x / 10));
}
int main()
{
int n, result;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
result = sum(n);
if (result % 10 == 0)
{
cout << result << endl;
}
else
{
}
}
c++
Why not simply--n % 9 + 1
? unless I've misread the question.
– Bathsheba
Jan 1 at 19:39
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52
add a comment |
so the question is:
**cant use loops in the recursive functions, only for input check
Write a program that uses a recursive function (or function) that receives a positive integer from the user and calculates the "final amount" of his digits.
The sum of the final digits of a number is the result of a process in which calculates the sum of digits of the number and if the sum is not a single digit number and return the sum the digits of the sum until you get a single digit number.
example :
The sum of the digits of 96437 is 29 9 + 6 + 4 + 3 + 7 = 29 And the sum of the digits of 29 is 11 2 + 9 = 11 And the sum of the digits of 11 is 2 1 + 1 = 2
I figured how to use recursion to calculate sum of a number but dont know how to put the right condition to do it so it will be single digit number.
Student for bioinformatics, tried to use if condition in main but cant think of something good.
#include <iostream>
using namespace std;
// recursive function to find sum of digits of a number
int sum(int x)
{
if (x == 0)
{
return 1;
}
return (x % 10 + sum(x / 10));
}
int main()
{
int n, result;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
result = sum(n);
if (result % 10 == 0)
{
cout << result << endl;
}
else
{
}
}
c++
so the question is:
**cant use loops in the recursive functions, only for input check
Write a program that uses a recursive function (or function) that receives a positive integer from the user and calculates the "final amount" of his digits.
The sum of the final digits of a number is the result of a process in which calculates the sum of digits of the number and if the sum is not a single digit number and return the sum the digits of the sum until you get a single digit number.
example :
The sum of the digits of 96437 is 29 9 + 6 + 4 + 3 + 7 = 29 And the sum of the digits of 29 is 11 2 + 9 = 11 And the sum of the digits of 11 is 2 1 + 1 = 2
I figured how to use recursion to calculate sum of a number but dont know how to put the right condition to do it so it will be single digit number.
Student for bioinformatics, tried to use if condition in main but cant think of something good.
#include <iostream>
using namespace std;
// recursive function to find sum of digits of a number
int sum(int x)
{
if (x == 0)
{
return 1;
}
return (x % 10 + sum(x / 10));
}
int main()
{
int n, result;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
result = sum(n);
if (result % 10 == 0)
{
cout << result << endl;
}
else
{
}
}
c++
c++
edited Jan 1 at 19:59
Gil
asked Jan 1 at 19:34
GilGil
82
82
Why not simply--n % 9 + 1
? unless I've misread the question.
– Bathsheba
Jan 1 at 19:39
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52
add a comment |
Why not simply--n % 9 + 1
? unless I've misread the question.
– Bathsheba
Jan 1 at 19:39
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52
Why not simply
--n % 9 + 1
? unless I've misread the question.– Bathsheba
Jan 1 at 19:39
Why not simply
--n % 9 + 1
? unless I've misread the question.– Bathsheba
Jan 1 at 19:39
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52
add a comment |
1 Answer
1
active
oldest
votes
if I well understand sum is just that :
int sum(int n)
{
return (n <= 9)
? n // the sum is the number itself
: sum((n % 10) + sum(n / 10));
}
int main()
{
int n;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
cout << sum(n) << endl;
return 0;
}
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998355%2fusing-recursion-functions-only-to-calculate-the-final-sum-of-a-given-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
if I well understand sum is just that :
int sum(int n)
{
return (n <= 9)
? n // the sum is the number itself
: sum((n % 10) + sum(n / 10));
}
int main()
{
int n;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
cout << sum(n) << endl;
return 0;
}
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
|
show 4 more comments
if I well understand sum is just that :
int sum(int n)
{
return (n <= 9)
? n // the sum is the number itself
: sum((n % 10) + sum(n / 10));
}
int main()
{
int n;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
cout << sum(n) << endl;
return 0;
}
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
|
show 4 more comments
if I well understand sum is just that :
int sum(int n)
{
return (n <= 9)
? n // the sum is the number itself
: sum((n % 10) + sum(n / 10));
}
int main()
{
int n;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
cout << sum(n) << endl;
return 0;
}
if I well understand sum is just that :
int sum(int n)
{
return (n <= 9)
? n // the sum is the number itself
: sum((n % 10) + sum(n / 10));
}
int main()
{
int n;
// input and input check for positive number
do{
cout << "Please enter a positive number:"<< endl;
cin >> n;
cout << endl;
} while (n <= 0);
cout << sum(n) << endl;
return 0;
}
answered Jan 1 at 19:54
brunobruno
8,54121024
8,54121024
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
|
show 4 more comments
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
thanks for your answer, but for 10 it gives 0
– Gil
Jan 1 at 20:18
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
@Gil not at all, with 10 its gives 1
– bruno
Jan 1 at 20:31
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
why did you put question mark at the rec function?
– Gil
Jan 1 at 20:35
@Gil it is just a more elegant way to write that :
if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
@Gil it is just a more elegant way to write that :
if (n <= 9) return n; else return sum((n % 10) + sum(n / 10));
– bruno
Jan 1 at 20:38
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
I see, we didnt learn it though, and could you explain the condition in the return please? thank you bruno
– Gil
Jan 1 at 20:40
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998355%2fusing-recursion-functions-only-to-calculate-the-final-sum-of-a-given-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not simply
--n % 9 + 1
? unless I've misread the question.– Bathsheba
Jan 1 at 19:39
Your code is wrong - the sum(10) will be 2 - as x==0 -> return 1; should be return 0;
– Andreas Rehm
Jan 1 at 21:52