lab_quacks
Spiteful Stacks and Questionable Queues
RecursionExercises Namespace Reference

Namespace to contain the recursion exercise code. More...

Functions

int sumDigits (int n)
 Given a non-negative int n, return the sum of its digits recursively (no loops). More...
 
int triangle (int rows)
 We have triangle made of blocks. More...
 

Detailed Description

Namespace to contain the recursion exercise code.

Function Documentation

int RecursionExercises::sumDigits ( int  n)

Given a non-negative int n, return the sum of its digits recursively (no loops).

Parameters
nThe number to sum the digits of
Returns
The sum of its digits
Note
Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).

Example: sumDigits(126) == 9 sumDigits(49) == 13 sumDigits(12) == 3

int RecursionExercises::triangle ( int  rows)

We have triangle made of blocks.

The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.

Parameters
rowsThe number of horizontal rows in the triangle.
Returns
The total number of blocks in the triangle pyramid.