You are given an integer n. Return the largest digit present in the number.
Input: n = 25
Output: 5
Explanation: The largest digit in 25 is 5.
Input: n = 99
Output: 9
Explanation: The largest digit in 99 is 9.
Input: n = 1
Given a number, all the digits can be extracted from the back (right) successively and the maximum of all the digits can be found by comparing every digit.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to find the largest
digit in a given number*/
int largestDigit(int n) {
// Variable to store the largest digit
int largestDigit = 0;
/* Keep on iterating while there
are digits left to extract */
while(n > 0) {
int lastDigit = n % 10;
/* If the current digit is greater than
largest digit, update largest digit */
if(lastDigit > largestDigit) {
largestDigit = lastDigit;
}
n = n / 10;
}
// Return the largest digit
return largestDigit;
}
};
int main()
{
int n = 348;
/* Creating an instance of
Solution class */
Solution sol;
// Function call to find the largest digit in n
int ans = sol.largestDigit(n);
cout << "The largest digit in the number is: " << ans;
return 0;
}
class Solution {
/* Function to find the largest
digit in a given number */
public int largestDigit(int n) {
// Variable to store the largest digit
int largestDigit = 0;
/* Keep on iterating while there
are digits left to extract */
while (n > 0) {
int lastDigit = n % 10;
/* If the current digit is greater than
largest digit, update largest digit */
if (lastDigit > largestDigit) {
largestDigit = lastDigit;
}
n = n / 10;
}
// Return the largest digit
return largestDigit;
}
public static void main(String[] args) {
int n = 348;
/* Creating an instance of
Solution class */
Solution sol = new Solution();
// Function call to find the largest digit in n
int ans = sol.largestDigit(n);
System.out.println("The largest digit in the number is: " + ans);
}
}
class Solution:
# Function to find the largest
# digit in a given number
def largestDigit(self, n):
# Variable to store the largest digit
largestDigit = 0
# Keep on iterating while there
# are digits left to extract
while n > 0:
lastDigit = n % 10
# If the current digit is greater than
# largest digit, update largest digit
if lastDigit > largestDigit:
largestDigit = lastDigit
n = n // 10
# Return the largest digit
return largestDigit
if __name__ == "__main__":
n = 348
# Creating an instance of
# Solution class
sol = Solution()
# Function call to find the largest digit in n
ans = sol.largestDigit(n)
print("The largest digit in the number is:", ans)
class Solution {
/* Function to find the largest
digit in a given number */
largestDigit(n) {
// Variable to store the largest digit
let largestDigit = 0;
/* Keep on iterating while there
are digits left to extract */
while (n > 0) {
let lastDigit = n % 10;
/* If the current digit is greater than
largest digit, update largest digit */
if (lastDigit > largestDigit) {
largestDigit = lastDigit;
}
n = Math.floor(n / 10);
}
// Return the largest digit
return largestDigit;
}
}
const n = 348;
/* Creating an instance of
Solution class */
const sol = new Solution();
// Function call to find the largest digit in n
const ans = sol.largestDigit(n);
console.log("The largest digit in the number is:", ans);
Time Complexity: O(log10(N)) – In every iteration, N is divided by 10 (equivalent to the number of digits in N.)
Space Complexity: O(1) – Using a couple of variables i.e., constant space.