Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
decimal_to_binary_recursion.c File Reference

Convert decimal to binary using recursion algorithm. More...

#include <assert.h>
Include dependency graph for decimal_to_binary_recursion.c:

Functions

int decimal_to_binary (unsigned int number)
 Decimal to binary using recursion algorithm. More...
 
void test ()
 Test function. More...
 
int main ()
 Driver Code. More...
 

Detailed Description

Convert decimal to binary using recursion algorithm.

Function Documentation

◆ decimal_to_binary()

int decimal_to_binary ( unsigned int  number)

Decimal to binary using recursion algorithm.

For example, if number = 5, the function returns the decimal integer 101.

Parameters
numberpositive integer number to convert
Returns
integer with digits representing binary value representation of number.
15{
16 return number == 0 ? 0 : number % 2 + 10 * decimal_to_binary(number / 2);
17}
int decimal_to_binary(unsigned int number)
Decimal to binary using recursion algorithm.
Definition: decimal_to_binary_recursion.c:14
Here is the call graph for this function:

◆ main()

int main ( void  )

Driver Code.

35{
36 test();
37 return 0;
38}
void test()
Test function.
Definition: decimal_to_binary_recursion.c:20
Here is the call graph for this function:

◆ test()

static void test ( void  )

Test function.

Self-test implementations.

Returns
void
21{
22 const int sets[][2] = {
23 {0, 0}, {1, 1}, {2, 10}, {3, 11}, {4, 100}, {6, 110}, {7, 111},
24 /* add more data sets to test */
25 };
26
27 for (int i = 0, size = sizeof(sets) / sizeof(sets[0]); i < size; ++i)
28 {
29 assert(decimal_to_binary(sets[i][0]) == sets[i][1]);
30 }
31}
Here is the call graph for this function: