recursion in cpp with reference
C++
void sum_digits(int & n, int & sum)
{
if ( n == 0 ) return;
sum += n % 10;
n /= 10;
sum_digits(n, sum);
}
#include <iostream>
using namespace std;
int main()
{
int n, sum=0;
cout << "enter a non-negative number" << endl;
cin >> n;
if ( n < 0 ) return -1; // don't trust the user
sum_digits(n,sum);
cout << "sum is " << sum << endl;
}#include <iostream>
#include <cstdlib> //had to force it becasue my compiler (Code::Blocks) does not contain system.
using namespace std;
/*int n = 1, sum = 0;
int sumDigits(int n, int sum)
{
//
if (n == 0)
{
return sum;
}
else
{
// applying recursion and returning the value into the function
sum = sum + n%10;
n= n/10;
return sumDigits(n, sum);
}
}
int main(int argc, char* argv[])
{
n = 1, sum = 0;
cout << "Enter a non-negative integer: ";
cin >> n;
sum = sumDigits (n, sum);
cout << "The sum of all digits "<< n << " is: " << sum << endl;
system ("PAUSE");
return 0;
}
*/
int sumDigits(int &);
int main()
{
int n;
sumDigits(n);
}
int sumDigits(int &n)
{
cout << "Enter a non-negative integer: ";
cin >> n;
if (n == 1)
{
return 1;
}
else
{
return (n - 1) + n;
}
cout << "The sum of all digits "<< n << " is: " << n << endl;
system ("PAUSE");
return 0;
}
Also in C++:
- Title
- vprintf
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- get data from terminal c++
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- mysqli connect
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- c++ print one line to console instead of multiple
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- tellg and seekg c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- binary search in set c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- c++ max of array
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- modulo c++
- Category
- C++
- Title
- how to create object in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- log base 10 c+_+
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- switch c++
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- c++ isalphanum
- Category
- C++
- Title
- remove value from vector c++
- Category
- C++
- Title
- char vector to string c++
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- unsorted array to bst
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- stoi c++
- Category
- C++
- Title
- sort function in vector c++ stl
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++