## 0 Replies - 10710 Views - Last Post: 22 December 2017 - 09:54 PMRate Topic:     //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408352&amp;s=4df67deb4c01e8d60103addd6d8538f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fatihmert Reputation: 1
• Posts: 129
• Joined: 04-March 12

# Recursive Samples

Posted 22 December 2017 - 09:54 PM

Hi guys,

I was writed a lot of recursive functions. I want just share its.

```#include "stdafx.h"

int multiple(int base, int each){
return each == 0 || base == 0 ? 0 : base + multiple(base, each - 1);
}

int factoriel(int n){
return n == 0 ? 1 : n * factoriel(n - 1);
}

int power(int base, int super){
return base == 0 || super == 0 ? 1 : base * power(base, super - 1);
}

int range_sum(int start, int end){
int current = start;
return current < end ? current + range_sum(current + 1, end) : current;
}

int fibonacci(int n){
return n == 0 || n == 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}

int _tmain(int argc, _TCHAR* argv[])
{
printf("5x5\t=\t%d\n", multiple(5,5)); //25
printf("5!\t=\t%d\n", factoriel(5)); //120
printf("13^2\t=\t%d\n", power(13,2)); //169
printf("sum(1 to 20)\t=\t%d\n", range_sum(1, 20));
printf("fib(30)\t=\t%d\n", fibonacci(1, 20));

return 0;
}

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }