0 Replies - 10710 Views - Last Post: 22 December 2017 - 09:54 PM Rate Topic: -----

#1 fatihmert   User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • 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