Largest and smallest digit of a number

14 Jul, 2022
Given a number N. The task is to find the largest and the smallest digit of the number.Examples :
Input : N = 2346 Output : 6 2 6 is the largest digit and 2 is smallestInput : N = 5 Output : 5 5
Approach: An efficient approach is to find all digits in the given number and find the largest and the smallest digit. 
C++
Java
Python3
C#
Javascript
// CPP program to largest and smallest digit of a number#include <bits/stdc++.h>using namespace std; // Function to the largest and smallest digit of a numbervoid Digits(int n){    int largest = 0;    int smallest = 9;     while (n) {        int r = n % 10;         // Find the largest digit        largest = max(r, largest);         // Find the smallest digit        smallest = min(r, smallest);         n = n / 10;    }    cout << largest << " " << smallest;} // Driver codeint main(){    int n = 2346;     // Function call    Digits(n);     return 0;}
// Java program to largest and smallest digit of a numberimport java.util.*;import java.lang.*;import java.io.*; class Gfg{     // Function to the largest and smallest digit of a numberstatic void Digits(int n){    int largest = 0;    int smallest = 9;     while(n != 0)    {        int r = n % 10;         // Find the largest digit        largest = Math.max(r, largest);         // Find the smallest digit        smallest = Math.min(r, smallest);         n = n / 10;    }    System.out.println(largest + " " + smallest);} // Driver codepublic static void main (String[] args) throws java.lang.Exception{    int n = 2346;     // Function call    Digits(n); }} // This code is contributed by nidhiva
# Python3 program to largest and smallest digit of a number # Function to the largest and smallest digit of a numberdef Digits(n):    largest = 0    smallest = 9     while (n):        r = n % 10         # Find the largest digit        largest = max(r, largest)         # Find the smallest digit        smallest = min(r, smallest)         n = n // 10     print(largest,smallest)  # Driver code n = 2346 # Function callDigits(n) # This code is contributed by mohit kumar 29
// C# program to largest and// smallest digit of a numberusing System;     class GFG{     // Function to the largest and// smallest digit of a numberstatic void Digits(int n){    int largest = 0;    int smallest = 9;     while(n != 0)    {        int r = n % 10;         // Find the largest digit        largest = Math.Max(r, largest);         // Find the smallest digit        smallest = Math.Min(r, smallest);         n = n / 10;    }    Console.WriteLine(largest + " " + smallest);} // Driver codepublic static void Main (String[] args){    int n = 2346;     // Function call    Digits(n);}} // This code is contributed by PrinciRaj1992
<script> // Javascript program to largest and// smallest digit of a number // Function to the largest and smallest// digit of a numberfunction Digits(n){    let largest = 0;    let smallest = 9;     while (n) {        let r = n % 10;         // Find the largest digit        largest = Math.max(r, largest);         // Find the smallest digit        smallest = Math.min(r, smallest);         n = parseInt(n / 10);    }    document.write(largest + " " + smallest);} // Driver code    let n = 2346;     // Function call    Digits(n); </script>
6 2
 
Time Complexity: O(log(n)), where n is the given numberAuxiliary Space: O(1)
Approach : Using str(),min(),max()
Python3
# Python3 program to largest and smallest digit of a number # Function to the largest and smallest digit of a numbern=2346s=str(n)print(max(s),end=" ")print(min(s))
6 2
nidhiva
mohit kumar 29
princiraj1992
Akanksha_Rai
rishavmahato348
sumitgumber28
shivamanandrj9
kogantibhavya
number-digits
Numbers
school-programming
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.