Using recursion functions only to calculate the final sum of a given number












-2















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
{

}

}









share|improve this question

























  • 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
















-2















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
{

}

}









share|improve this question

























  • 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














-2












-2








-2








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
{

}

}









share|improve this question
















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:59







Gil

















asked Jan 1 at 19:34









GilGil

102




102













  • 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













  • 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












1 Answer
1






active

oldest

votes


















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





share|improve this answer
























  • 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













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


}
});














draft saved

draft discarded


















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









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





share|improve this answer
























  • 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


















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





share|improve this answer
























  • 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
















0












0








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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 19:54









brunobruno

10.4k21126




10.4k21126













  • 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











  • @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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$