Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int findMaxDigit(int n);
- int findMaxDigitRecursive(int n);
- using namespace std;
- int main() {
- // cout << findMaxDigit(14286312);
- cout << findMaxDigitRecursive(14286312);
- return 0;
- }
- int findMaxDigit(int n) {
- int maxDigit = n % 10;
- n = n/10;
- while (n != 0) {
- int lastDigit = n % 10;
- if (maxDigit < lastDigit) {
- maxDigit = lastDigit;
- }
- n = n /10;
- }
- return maxDigit;
- }
- int findMaxDigitRecursive(int n) {
- if (n == 0) {
- return 0;
- } else {
- int lastDigit = n % 10;
- int maxOfRemainingDigits = findMaxDigitRecursive(n /10);
- if (lastDigit > maxOfRemainingDigits) {
- return lastDigit;
- } else {
- return maxOfRemainingDigits;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement