## finding the largest digit in an integer using recursion

java program to find the largest and smallest digit of a number

find largest number in array using recursion java

java program to find smallest digit in a number

c program to find smallest digit in a number

find second largest number in array using recursion

largest possible number with digits in java

max recursion java

I have a practice who's task is to find the largest digit in an integer using recursion in java. For example, for the number 13441 the digit '4' will be returned.

I have been trying for a day now and nothing worked.

What I thought could work is the following code, which I can't quite get the "base case" for:

public static int maxDigit(int n) { int max; if (n/100==0) { if (n%10>(n/10)%10) { max=n%10; } else max=(n/10)%10; } else if (n%10>n%100) max=n%10; else max=n%100; return maxDigit(n/10); }

As you can see it's completely wrong.

Any help would be great. Thank you

The simplest base case, is that if n is 0, return 0.

public static int maxDigit(int n){ if(n==0) // Base case: if n==0, return 0 return 0; return Math.max(n%10, maxDigit(n/10)); // Return max of current digit and // maxDigit of the rest }

or, slightly more concise;

public static int maxDigit(int n){ return n==0 ? 0 : Math.max(n%10, maxDigit(n/10)); }

**Java Program to find the largest digit of a number using recursion,** Example: How to find the largest digit of a number using recursion in Java. Run it Now. import java.util.Scanner; class Main { int lar = 0, r; int largestDigit(int n) { if Largest number less than N with digit sum greater than the digit sum of N; Generate a number such that the frequency of each digit is digit times the frequency in given number; Decimal to binary number using recursion; Count Set-bits of number using Recursion; Number of times a number can be replaced by the sum of its digits until it only contains one digit; Program to find all Factors of a Number using recursion; Largest number less than N whose each digit is prime number

This works by recursively comparing the right most digit with the highest digit of the remaining digits (those being obtained by dividing the original number by 10):

int maxDigit(int n) { n = Math.abs(n); // make sure n is positive if (n > 0) { int digit = n % 10; int max = maxDigit(n / 10); return Math.max(digit, max); } else { return 0; } }

**Highest Digit in a number(recursive func - C++ Forum,** #include <iostream> using namespace std; int m( int n) { int a,b; if (n==0) return 0; else { a = n%10; b = m(n/10); if (a>b) return a; else return b; } C Program to find the largest Element in an Array using Recursion. Below is a program to find the largest array element in a given array using recursion. #define is used to initialize a value which is more like a constant.

I won't dig into your code, which I think is more complicated than it has to be. But it seems to me that the cases are actually fairly simple (unless I'm missing something):

base case: parameter only has one digit, return that one digit as parameter

general case: return whichever is higher of (the first digit in the parameter) and (the maxDigit of the remaining digits in the parameter)

**Largest and smallest digit of a number,** Given a number N. The task is to find the largest and the smallest digit of the number. Largest number less than N with digit sum greater than the digit sum of N Java Program to Find Largest Number in Array Using Recursion. Here you will get java program to find largest number in array using recursion. int a[] = {5, 12, 10, 6, 15}; System.out.println("Given Array: "); System.out.print(a[i] + " "); System.out.println(" Largest Number is " + findLargest(a, a.length-1));

You may also write:

public static int maxDigit(int n, int max){ if(n!=0) { if(n%10 > max) { max = n%10; } return maxDigit(n/10, max); } return max; }

**Find maximum number that can be formed using digits of a given ,** Recursive sum of digits of a number formed by repeated appends · Check if the number formed by the last digits of N numbers is divisible by 10 or not · Find Given an array of integers, we need to find the minimum element of that array using recursion. If there is single element, return it. Else return minimum of following. a) Last Element b) Value returned by recursive call fir n-1 elements. If there is single element, return it. Else return maximum of following.

**Largest Digit in a Number,** C program to find the largest digit in a number. 5 Make the greatest and the smallest 4 digit Duration: 3:57
Posted: Apr 5, 2018 (a recursion can lead to an infinite loop, if the base case is not met in the calls). For this problem, the "base case" is: if number == 0: return 0 A simple recursive function for sum all the digits of a number is: def sum_digits(number): """ Return the sum of digits of a number.

**Reduce a number by its largest digit,** Find the largest digit, D; Remove D from the list. If there is more than one occurrence of D, Takes input as a string representation of an integer, like f("26364") . Bagwise subtraction, used for removing the highest digit, with Z ~ The current Try it online! Recursive lambda function called like f[26364] . For one of the questions i was asked to solve, I found the max value of an array using a for loop, so i tried to find it using recursion and this is what I came up with: public static int findMax(

**C program to count digits of a number using recursion,** Given an integer number and we have to count the digits using recursion count++; countDigits(num/10); } else { return count; } } int main() { int number; int C program to find largest digit of a number. Write a C program to find largest digit of a number. #include <stdio.h>. int main() {. int num, large = 0, rem = 0; /* get the input from the user */.

##### Comments

- @Baadshah: I don't see why that's necessary for this question. The code is perfectly readable without.
- One thing to change is what you return. You should return the greater of
`max`

&`maxDigit(n/10)`

- Is this a homework assignment or something? I can't think of a worse place to use recursion.
- Your code as it stands now will go into infinite loop. There is no coming out from recursion.
- @Assaf in some languages (i.e. functional languages) recursion would be the natural way to solve this.
- this is technically the same algorithm as mine, albeit written slightly differently.
- awww. very lovely !
- I don't think
`int`

evaluates to`true`

or`false`

in java - if (n) is not valid java syntax
- oops - good point! I tested the algorithm using JS, but not the syntax in a real Java compiler. Mea culpa.