Advertisement
tyler569

lab2.c

Feb 5th, 2017
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. /*
  2.  * Recursive programming demonstration
  3.  * CNIT 315
  4.  * Tyler Philbrick
  5.  * 2017-02-03
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. /* Function prototypes */
  11. int factorial(int);
  12. int fibonacci(int);
  13. void hanoi(int, char, char, char);
  14.  
  15. int main() {
  16.     int sel;
  17.  
  18.     while (1) {
  19.  
  20.         printf("\nMenu:\n1: Factorial\n2: Fibonacci\n3: Tower of Hanoi\n");
  21.         printf("4: Exit\nMake a choice: ");
  22.         scanf("%i", &sel);
  23.         fflush(stdin);
  24.         while (sel > 4 || sel < 1) {
  25.             printf("Invalid choice. Try again: ");
  26.             scanf("%i", &sel);
  27.             fflush(stdin);
  28.         }
  29.    
  30.         /* Factorial */
  31.         if (1 == sel) {
  32.             int fac_n;
  33.             printf("Enter a desired number n for n! (0-12): ");
  34.             scanf("%i", &fac_n);
  35.             while (fac_n > 12 || fac_n < 0) {
  36.                 printf("Invalid choice. Try again: ");
  37.                 scanf("%i", &fac_n);
  38.             }
  39.             printf("\n%d! = %d\n", fac_n, factorial(fac_n));
  40.  
  41.         /* Fibonacci */
  42.         } else if (2 == sel) {
  43.             int fib_n;
  44.             printf("Enter a desired number n for F_n (0-46): ");
  45.             scanf("%i", &fib_n);
  46.             while (fib_n > 46 || fib_n < 0) {
  47.                 printf("Invalid choice. Try again: ");
  48.                 scanf("%i", &fib_n);
  49.             }
  50.             printf("\nF_%d = %d\n", fib_n, fibonacci(fib_n));
  51.  
  52.         /* Hanoi */
  53.         } else if (3 == sel) {
  54.             int han_l;
  55.             printf("Enter the number of disks for the Towe of Hanoi (1-1000): ");
  56.             scanf("%i", &han_l);
  57.             while (han_l > 1000 || han_l < 1) {
  58.                 printf("Invalid choice. Try again: ");
  59.                 scanf("%i", &han_l);
  60.             }
  61.             hanoi(han_l, 'A', 'B', 'C');
  62.  
  63.         } else if (4 == sel) {
  64.             printf("Good bye!\n");
  65.             break;
  66.  
  67.         } else {
  68.             printf("Invalid choice. Try again.\n");
  69.         }
  70.     }
  71.     return 0;
  72. }
  73.  
  74. /* Return the product of all positive integers less than
  75.  * or equal to n
  76.  */
  77. int factorial(int n) {
  78.     if(1 >= n) {
  79.         return 1;
  80.     } else {
  81.         return n * factorial(n-1);
  82.     }
  83. }
  84.  
  85. /* Return the n-th term of Fibonacci sequence */
  86. int fibonacci(int n) {
  87.     if(0 >= n) {
  88.         return 0;
  89.     } else if(1 == n) {
  90.         return 1;
  91.     } else {
  92.         return fibonacci(n-1) + fibonacci(n-2);
  93.     }
  94. }
  95.  
  96. /* Move n discs from peg_a to peg_c. */
  97. void hanoi(int n, char peg_a, char peg_b, char peg_c) {
  98.     if(1 == n) {
  99.         printf("Move the top disk: peg %c -> peg %c\n", peg_a, peg_c);
  100.     } else {
  101.         hanoi(n-1, peg_a, peg_c, peg_b);
  102.         hanoi(1, peg_a, peg_b, peg_c);
  103.         hanoi(n-1, peg_b, peg_a, peg_c);
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement