Advertisement
valve2

recursion"

May 31st, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. int subtract(int num);
  6. int power(int frstdigit, int thrddigit);
  7. void random();
  8. void randomtwo();
  9.  
  10. void random()
  11. {
  12.  
  13.     srand(time_t(NULL));
  14.     int num = (100 + rand() % (999 - 100 + 1));
  15.     printf("%d\n", num);
  16.     int third = num % 10;
  17.     int first = (num / 10) / 10;
  18.     int result = power(first, third);
  19.     printf("%d", result);
  20.     randomtwo();
  21. }
  22. int power(int frstdigit, int thrddigit)
  23. {
  24.     if (thrddigit == 0)
  25.         return 1;
  26.     else
  27.         return frstdigit * power(frstdigit, thrddigit - 1);
  28.  
  29. }
  30. void randomtwo()
  31. {
  32.     srand(time_t(NULL));
  33.     int rando = (10000 + rand() % (99999 - 10000 + 1));
  34.     printf("\n5 digit rando %d\n", rando);
  35.     int result = subtract(rando);
  36.     printf("\n%d\n", result);
  37.  
  38.  
  39. }
  40. int subtract(int num)
  41. {
  42.     if (num < 10)
  43.         return num;
  44.  
  45.  
  46.     int leftMostDigit = num % 10;
  47.     int remainingNumber = num / 10;
  48.  
  49.     int result = leftMostDigit - subtract(remainingNumber);
  50.  
  51.  
  52.     if (result < 0)
  53.         result *= -1;
  54.  
  55.     return result;
  56. }
  57. int main() {
  58.     random();
  59.     return 0;
  60. }
  61.  
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement