Deepeshkumar
Deepeshkumar

Reputation: 443

How to find greatest digit in a number using recursion?

I am trying to write a function in Java that returns the greatest digit in a number using recursion. I have managed to do it using two parameters, the number and greater digit. Initially the greater digit parameter accepts value as 0.

static int getGreatestDigit(int num , int greater){
    if(num != 0){
        if(num %10 > greater){
           greater = num%10;
           num = num/10;
           return getGreatestDigit(num , greater);
        }else{
           num = num/10;
           return getGreatestDigit(num , greater);
        }
    }
    return greater;
}

I want to write same recursive function but with only one parameter that is number. Like

int getGreatestDigit(int num){
 //code
}

I am stuck at logic. How to do that?

Upvotes: 3

Views: 5371

Answers (6)

suyash kumar
suyash kumar

Reputation: 1

public class Biggest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number");
        int n = sc.nextInt();
        sc.close();
        int big=getBiggest(n,n%10);
        System.out.println("Biggest digit in "+n+" is : " +big);
    }

private static int getBiggest(int n,int big) {//take big=n%10 in method caller
        
        if(n!=0)
            
        if(n%10>big) {
            big=n%10;
            n=n/10;
            return getBiggest(n,big);
        }
        else 
        {
          n=n/10;
          return getBiggest(n,big);
        }
         return big;
    }
     
    
}

Upvotes: 0

Maninder
Maninder

Reputation: 1919

    package Map;
    import java.util.ArrayList;
    public class Practice8 {
        public int highestDigit(int number){
            ArrayList<Integer> temp= new ArrayList<>();
            StringBuilder sb= new StringBuilder();
            sb.append(number);
            String value= sb.toString();
            for(int i=0;i<value.length();i++){
                temp.add((int)value.charAt(i)-'0');
            }
            int max=0;
            for(int x: temp){
                 if(x>max){
                    max=x;
                }
            }
            return max;
    
        }
        public static void main(String[] args) {
            Practice8 practice8= new Practice8();
            System.out.println(practice8.highestDigit(379));
        }
    }

Upvotes: -1

Daniel Jour
Daniel Jour

Reputation: 16156

You can do this, if you use the functions stack as temporary memory to hold your interim results, i.e. what was previously stored in the greater parameter.

This changes your function to be no longer tail recursive, making it worse performance wise.

int greatestDigit(int num) {
 int last = num % 10;
 int rest = num / 10;
 if (rest == 0) {
  return last;
 } else {
  int candidate = greatestDigit (rest);
  if (candidate > last) {
   return candidate;
  } else {
   return last;
  }
 }
}

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35011

/** Pseudocode:
1. if num > 9, /10 and call getGreatestDigit on that (recursive step). Then get the current digit (undivided) and return the greater of the two
2. if num <= 9, return num
*/

int getGreatestDigit(int num){
 //code
}

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

Only the first call to getGreatestDigit(num) needs to keep track of the greater result. Each recursive call to getGreatestDigit(num) will return the greatest digit in the part of the original number that it is tasked with scanning. The very first invocation of getGreatestDigit(num) can compare the number it took with the greatest number returned from all recursive calls.

int getGreatestDigit(int num)
{
    if (num == 0) return 0;
    int lastNum = num % 10;
    int otherDigits = num / 10;
    int recursiveLastNum = getGreatestDigit(otherDigits);
    return Math.Max(lastNum, recursiveLastNum);
}

Upvotes: 4

Jashaszun
Jashaszun

Reputation: 9270

static int getGreatestDigit(int num)
{
    return num == 0 ? 0 :
        Math.Max(num % 10, getGreatestDigit(num / 10));
}

So basically, you look at the least significant digit each time, comparing it against the maximum of the rest of the digits.

Upvotes: 3

Related Questions