lab_quacks
Spiteful Stacks and Questionable Queues
All Classes Namespaces Files Functions
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)
 Compute the total number of blocks in a triangle of blocks. More...
 

Detailed Description

Namespace to contain the recursion exercise code.

Function Documentation

◆ sumDigits()

int RecursionExercises::sumDigits ( int  n)

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

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).

Examples:

  • sumDigits(126) == 9
  • sumDigits(49) == 13
  • sumDigits(12) == 3
Parameters
nThe number to sum the digits of
Returns
The sum of its digits

◆ triangle()

int RecursionExercises::triangle ( int  rows)

Compute the total number of blocks in a triangle of blocks.

In a triangle of blocks, the topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on.

* 1 block
* * 2 blocks
* * * 3 blocks
* * * * 4 blocks
............... n blocks
Parameters
rowsThe number of horizontal rows in the triangle
Returns
The total number of blocks in the triangle pyramid