c 2b 2b recursion

Solutions on MaxInterview for c 2b 2b recursion by the best coders in the world

showing results for - "c 2b 2b recursion"
Matilda
11 Aug 2020
1#include <iostream>
2#include <cstdlib> //had to force it becasue my compiler (Code::Blocks) does not contain system.
3
4using namespace std;
5/*int n = 1, sum = 0;
6
7int sumDigits(int n, int sum)
8{
9    //
10	if (n == 0)
11    {
12        return sum;
13    }
14    else
15    {
16        // applying recursion and returning the value into the function
17        sum = sum + n%10;
18		n= n/10;
19        return sumDigits(n, sum);
20    }
21}
22
23int main(int argc, char* argv[])
24{
25	n = 1, sum = 0;
26
27        cout << "Enter a non-negative integer: ";
28        cin >> n;
29        sum = sumDigits (n, sum);
30        cout << "The sum of all digits "<< n << " is: " << sum << endl;
31
32	system ("PAUSE");
33
34        return 0;
35}
36*/
37
38int sumDigits(int &);
39
40int main()
41{
42	int n;
43	sumDigits(n);
44}
45
46int sumDigits(int &n)
47{
48    cout << "Enter a non-negative integer: ";
49    cin >> n;
50        if (n == 1)
51        {
52            return 1;
53        }
54        else
55        {
56            return (n - 1) + n;
57        }
58    cout << "The sum of all digits "<< n << " is: " << n << endl;
59
60
61	system ("PAUSE");
62
63        return 0;
64}
Mattia
06 Jul 2018
1//recursion in c++
2//factorial
3#include<iostream>
4#include<bits/stdc++.h>
5using namespace std;
6
7int factorialfun(int num)
8{
9	if (num>0)
10	{
11		return num*factorialfun(num-1);
12	}
13	else
14	{
15		return 1;
16	}
17}
18int main()
19{
20	int num;
21	cin>>num;
22	cout<<factorialfun(num);
23}
24
Andrea
12 Feb 2017
1//AUTHOR:praveen
2//Function calling itself 
3//Example in c++
4#include<iostream>
5using namespace std;
6int recursion(int a){
7  	if(a==1)//BASE CASE
8      return 0;
9	cout<<a;
10  	a=a-1;
11  	return recursion(a);//FUNCTION CALLING ITSELF
12}
13int main(){
14  	int a=5; 
15	recursion(a);
16  	return 0;
17}
18//OUTPUT: 5 4 3 2 
Morgane
25 Jul 2020
1void sum_digits(int & n, int & sum)
2{
3  if ( n == 0 ) return;
4  sum += n % 10;
5  n /= 10;
6  sum_digits(n, sum);
7}
8
9#include <iostream>
10using namespace std;
11
12int main()
13{
14  int n, sum=0;
15  cout << "enter a non-negative number" << endl;
16  cin >> n;
17  if ( n < 0 ) return -1; // don't trust the user
18  sum_digits(n,sum);
19  cout << "sum is " << sum << endl;
20}
Mira
06 Sep 2018
1// Factorial of n = 1*2*3*...*n
2#include <iostream>
3using namespace std;
4
5int factorial(int);
6
7int main() {
8    int n, result;
9
10    cout << "Enter a non-negative number: ";
11    cin >> n;
12
13    result = factorial(n);
14    cout << "Factorial of " << n << " = " << result;
15    return 0;
16}
17
18int factorial(int n) {
19    if (n > 1) {
20        return n * factorial(n - 1);
21    } else {
22        return 1;
23    }
24}
queries leading to this page
cpp recursion 3fhow to create recursive function in c 2b 2bprogramming recursion c 2b 2b compilercpp recursionrecursive call c 2b 2bcpp recursive functioneswhat is recursive function in c 2b 2buse of recursion in c 2b 2brecursive function c 2b 2bdefine recursion in c 2b 2b programmingc c 2b 2b recursionrecursive example in c 2b 2brecursion in loops c 2b 2brecursion constructor c 2b 2bsimple recursion example c 2b 2brecursion guide c 2b 2brecursive function example c 2b 2bwhat is a recursive function c 2b 2b simple types of recursion in c 2b 2bc 2b 2b how to make a recursive referencerecursion in c 2b 2bhow to create recursive function in cpphow to master recursion in c 2fc 2b 2bwhat is recursive function cpprecursion programs in c 2b 2brecurssion cppc 2b 2b recursive functionc 28n 2c k 29 recursion c 2b 2brecursive function call c 2b 2bwhen we have to use recursion in c 2b 2brecursive function cppstruct recursion c 2b 2brecursive function cpp examplerecursive functions in c 2b 2balgorithm recursion c 2b 2brecursion cpp examplereturn recursive call c 2b 2bc 2b 2b recursion examplesc 2b 2b recursion examplerecursion c 2b 2b examplesrecursion c 2b 2b coderecursive function c 2b 2brecursive code c 2b 2brecursion example c 2b 2bc 2b 2b recursive usinghowotodo c 2b 2b recursionrecursion in c 2b 2b cppnutshow recursion works c 2b 2brecursion examples c 2b 2b sample programsconstructor recursion in c 2b 2bgood examples of recursion c 2b 2brecursion function c 2b 2b 2b 2brecursive function clearn recursion c 2b 2bc 2b 2b recursionuse of recursive function in c 2b 2bfunction that calls itself c 2b 2bdefine recursion in c 2b 2bprint with recursion in c 2b 2bcpp recursive functionrecursion c 2b 2b explainedrecursion programs c 2b 2bc 2b 2b using recursionall c 2b 2b function are recursiveuse function in itself c 2b 2brecursion c 2b 2b tutorials c 2b 2b fun recursion examplesrecursion elements c 2b 2bc 2b 2b program recursionrecursion in c 2b 2b programizfunction recursion in c 2b 2brecursion c 2b 2b practicerecursion in functors in cpprecursion in c 2b 2brecursion questions in c 2b 2brecurisve function in c 2b 2busing recursion in c 2b 2b question of recursive function in c 2b 2brecursive function in c 2b 2bc 2b 2b stl learn for coding and recursion recursion function in cpprecursion meaning in cppwhat does recursive mean c 2b 2brecusively call c 2b 2bdata structure function recursion c 2b 2b programsrecursion in function c 2b 2brecursion c 2b 2bthe uses of recursion in c 2b 2brules for recursive function in c 2b 2bc 2b 2b include recursionrecursion code in c 2b 2brecursion in c 2b 2b codec 2b 2b recursion explainedc 2b 2b function recursionrecursion examples c 2b 2b some recursin c 2b 2brecursion solutions cpprecurssion in cppwhat is recursion in c 2b 2bbasic recursion functions c 2b 2brecursion definition c 2b 2bwhat is a recursive function c 2b 2b recursion in c 2b 2b serieswhere can you not perform recursion in c 2b 2brecursion examples in c 2b 2bwhat is recursion in cpprecurrosive function c 2b 2brecursion in a class in c 2b 2brecursive function in cppall c 2b 2b function are recursiondefine recursive function in c 2b 2b with examplerekursion c 2b 2bhow to write a recursive function in c 2b 2bhow to do recursion c 2b 2brecursion in cpprecursion inc 2b 2bexamples of recursion in c 2b 2bexample c 2b 2b programms on recurssionrecursion function in c 2b 2bis there recursion in c 2b 2brecurssion cp algorithmsrecursive method c 2b 2blearn recursion in c 2b 2bwhat is recursion function in c 2b 2brecursion on c 2b 2bdirectly recursive function c 2b 2brecursive call in c 2b 2brecursion calling recursion cpp explain about recursive function along with example in c 2b 2bhow to make recursive function c 2b 2brecusrive function c 2b 2bc 2b 2b code to call the recirsive funcitonrecursive cpprecursion simple program in c 2b 2brecursion cpphow does recursion work in c 2b 2bc 2b 2b program using recursionrecursion c 2b 2b program exampleshow to write a recursive c 2b 2brecursion programs c 2brecursion explained c 2b 2barray recursion in c 2b 2bsimple recursion examle in c 2b 2bcpp recursive functionsrecursion c 2b 2b examplehow does recursion work c 2b 2b recursive method cpp examplerecursion code c 2b 2bcpp recursion treewhat is recursion in c 2b 2brecursive function in c 2b 2brecursion program in c 2b 2b using classc 2b 2b recursion