1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <stdio.h>
#include <stdlib.h>
/* RECURSION
*
* The two functions below, iterative(…) and recursive(…) do the same thing.
*/
void iterative(int a) {
// counts all numbers iteratively (via a loop)
for(int i = a; i > 0; i--) {
printf("%d\n", i);
}
}
void recursive(int a) {
if(a > 0) {
printf("%d\n", a);
recursive(a - 1);
}
}
int main(int argc, char* argv[]) {
printf("iterative(…)\n");
iterative(10);
printf("\n");
printf("recursive(…)\n");
recursive(10);
return EXIT_SUCCESS;
}
/* OUTPUT:
iterative(…)
10
9
8
7
6
5
4
3
2
1
recursive(…)
10
9
8
7
6
5
4
3
2
1
*/
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2017 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.