Advertisement
bogdanNiculeasa

Find max digit recursiv and normal

Sep 23rd, 2021
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int findMaxDigit(int n);
  4. int findMaxDigitRecursive(int n);
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. //    cout << findMaxDigit(14286312);
  10.     cout << findMaxDigitRecursive(14286312);
  11.     return 0;
  12. }
  13.  
  14. int findMaxDigit(int n) {
  15.     int maxDigit = n % 10;
  16.     n = n/10;
  17.     while (n != 0) {
  18.         int lastDigit = n % 10;
  19.         if (maxDigit < lastDigit) {
  20.             maxDigit = lastDigit;
  21.         }
  22.         n = n /10;
  23.     }
  24.     return maxDigit;
  25. }
  26.  
  27. int findMaxDigitRecursive(int n) {
  28.     if (n == 0) {
  29.         return 0;
  30.     } else {
  31.         int lastDigit = n % 10;
  32.         int maxOfRemainingDigits = findMaxDigitRecursive(n /10);
  33.         if (lastDigit > maxOfRemainingDigits) {
  34.             return lastDigit;
  35.         } else {
  36.             return maxOfRemainingDigits;
  37.         }
  38.     }
  39. }
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement