informaticage

Introduction to recursion

May 3rd, 2021
1,319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int s_rec(int n);
  4. int pow_rec(int k, int n);
  5. int fact_rec(int n);
  6. int move_chars(int k, int d);
  7. int move_chars_i(int k, int d);
  8.  
  9. int main(void) {
  10.   // int n;
  11.   // scanf("%d", &n);
  12.   printf("move_chars: %c", move_chars_i('A', 5));
  13.   return 0;
  14. }
  15.  
  16. int s_rec(int n) {
  17.   if (n == 1)
  18.     return 1;
  19.  
  20.   return n + s_rec(n - 1);
  21. }
  22.  
  23. int pow_rec(int k, int n) {
  24.   if (n == 0)
  25.     return 1;
  26.  
  27.   return k * pow_rec(k, n - 1);
  28. }
  29.  
  30. int fact_rec(int n) {
  31.   if (n == 0)
  32.     return 1;
  33.   return n * fact_rec(n - 1);
  34. }
  35.  
  36. int move_chars_i(int k, int d) {
  37.   while(d > 0) {
  38.     k ++;
  39.     d --;
  40.   }
  41.   return k;
  42. }
  43.  
  44. int move_chars(int k, int d) {
  45.   printf("move_chars (%d, %d) -> ", k, d);
  46.   if (d == 0)
  47.     return k;
  48.  
  49.   return move_chars(k + 1, d - 1);
  50. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×