While you don't have to be a world-renowned mathematician to be a programmer, knowing how to manipulate variables' numbers is an invaluable skill to learn. Tricky problems based on numbers are common in programming interviews and quizzes. In this article, you'll learn how to find the largest and smallest digit of a number using Python, C++, JavaScript, C, and Java.

Problem Statement

You're given a number num. You need to find and print the largest and smallest digit of num.

Example 1: Let num = 238627

8 is the largest and 2 is the smallest digit of 238627.

Thus, the output is:

Largest Digit: 8

Smallest Digit: 2

Example 2: Let num = 34552

5 is the largest and 2 is the smallest digit of 34552.

Thus, the output is:

Largest Digit: 5

Smallest Digit: 2

Example 3: Let num = 123

3 is the largest and 1 is the smallest digit of 123.

Thus, the output is:

Largest Digit: 3

Smallest Digit: 1

C++ Program to Find the Largest and Smallest Digit of a Number

Below is the C++ program to find the largest and smallest digit of a number:

        // C++ program to find the largest and smallest
// digit of a number
#include <bits/stdc++.h>
using namespace std;

void findLargestSmallest(int num)
{
    int largestDigit = 0;
    int smallestDigit = 9;
    int digit;
    while(num)
    {
        digit = num%10;
        // Finding the largest digit
        largestDigit = max(digit, largestDigit);

        // Find the smallest digit
        smallestDigit = min(digit, smallestDigit);
        num = num/10;
    }
    cout << "Largest Digit: " << largestDigit << endl;
    cout << "Smallest Digit: " << smallestDigit << endl;
}

// Driver Code
int main()
{
    int num1 = 238627;
    cout << "num1: " << num1 << endl;
    findLargestSmallest(num1);

    int num2 = 34552;
    cout << "num2: " << num2 << endl;
    findLargestSmallest(num2);

    int num3 = 123;
    cout << "num3: " << num3 << endl;
    findLargestSmallest(num3);

    int num4 = 45672;
    cout << "num4: " << num4 << endl;
    findLargestSmallest(num4);

    int num5 = 76567;
    cout << "num5: " << num5 << endl;
    findLargestSmallest(num5);

    return 0;
}

Output:

        num1: 238627
Largest Digit: 8
Smallest Digit: 2
num2: 34552
Largest Digit: 5
Smallest Digit: 2
num3: 123
Largest Digit: 3
Smallest Digit: 1
num4: 45672
Largest Digit: 7
Smallest Digit: 2
num5: 76567
Largest Digit: 7
Smallest Digit: 5

Related: How to Find the Mean of an Array in Python, C++, JavaScript, and C

Python Program to Find the Largest and Smallest Digit of a Number

Below is the Python program to find the largest and smallest digit of a number:

        # Python program to find the largest and smallest
# digit of a number

def findLargestSmallest(num):
    largestDigit = 0
    smallestDigit = 9

    while (num):
        digit = num % 10

        # Finding the largest digit
        largestDigit = max(digit, largestDigit)

        # Finding the smallest digit
        smallestDigit = min(digit, smallestDigit)

        num = num // 10
    print("Largest Digit:", largestDigit)
    print("Smallest Digit:", smallestDigit)

# Driver Code
num1 = 238627
print("num1:", num1)
findLargestSmallest(num1)

num2 = 34552
print("num2:", num2)
findLargestSmallest(num2)

num3 = 123
print("num3:", num3)
findLargestSmallest(num3)

num4 = 45672
print("num4:", num4)
findLargestSmallest(num4)

num5 = 76567
print("num5:", num5)
findLargestSmallest(num5)

Output:

        num1: 238627
Largest Digit: 8
Smallest Digit: 2
num2: 34552
Largest Digit: 5
Smallest Digit: 2
num3: 123
Largest Digit: 3
Smallest Digit: 1
num4: 45672
Largest Digit: 7
Smallest Digit: 2
num5: 76567
Largest Digit: 7
Smallest Digit: 5

Related: How to Convert Time in 12-Hour Format to 24-Hour Format With Programming

JavaScript Program to Find the Largest and Smallest Digit of a Number

Below is the JavaScript program to find the largest and smallest digit of a number:

        // JavaScript program to find the largest and smallest
// digit of a number

function findLargestSmallest(num) {
    var largestDigit = 0;
    var smallestDigit = 9;
    var digit;
    while(num) {
        digit = num%10;
        // Finding the largest digit
        largestDigit = Math.max(digit, largestDigit);

        // Find the smallest digit
        smallestDigit = Math.min(digit, smallestDigit);
        num = parseInt(num / 10);
    }
    document.write("Largest Digit: " + largestDigit + "
");
    document.write("Smallest Digit: " + smallestDigit + "
");
}

// Driver Code
var num1 = 238627;
document.write("num1: " + num1 + "
");
findLargestSmallest(num1);

var num2 = 34552;
document.write("num2: " + num2 + "
");
findLargestSmallest(num2);

var num3 = 123;
document.write("num3: " + num3 + "
");
findLargestSmallest(num3);

var num4 = 45672;
document.write("num4: " + num4 + "
");
findLargestSmallest(num4);

var num5 = 76567;
document.write("num5: " + num5 + "
");
findLargestSmallest(num5);

Output:

        num1: 238627
Largest Digit: 8
Smallest Digit: 2
num2: 34552
Largest Digit: 5
Smallest Digit: 2
num3: 123
Largest Digit: 3
Smallest Digit: 1
num4: 45672
Largest Digit: 7
Smallest Digit: 2
num5: 76567
Largest Digit: 7
Smallest Digit: 5

Related: How to Print All Permutations of a Given String in C, C++, JavaScript, and Python

C Program to Find the Largest and Smallest Digit of a Number

Below is the C program to find the largest and smallest digit of a number:

        // C program to find the largest and smallest
// digit of a number
#include <stdio.h>
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a>b?b:a)

void findLargestSmallest(int num)
{
    int largestDigit = 0;
    int smallestDigit = 9;
    int digit;
    while(num)
    {
        digit = num%10;
        // Finding the largest digit
        largestDigit = Max(digit, largestDigit);

        // Find the smallest digit
        smallestDigit = Min(digit, smallestDigit);
        num = num/10;
    }
    printf("Largest Digit: %d \⁠n", largestDigit);
    printf("Smallest Digit: %d \⁠n", smallestDigit);
}

// Driver Code
int main()
{
    int num1 = 238627;
    printf("num1: %d \⁠n", num1);
    findLargestSmallest(num1);

    int num2 = 34552;
    printf("num2: %d \⁠n", num2);
    findLargestSmallest(num2);

    int num3 = 123;
    printf("num3: %d \⁠n", num3);
    findLargestSmallest(num3);

    int num4 = 45672;
    printf("num4: %d \⁠n", num4);
    findLargestSmallest(num4);

    int num5 = 76567;
    printf("num5: %d \⁠n", num5);
    findLargestSmallest(num5);

    return 0;
}

Output:

        num1: 238627
Largest Digit: 8
Smallest Digit: 2
num2: 34552
Largest Digit: 5
Smallest Digit: 2
num3: 123
Largest Digit: 3
Smallest Digit: 1
num4: 45672
Largest Digit: 7
Smallest Digit: 2
num5: 76567
Largest Digit: 7
Smallest Digit: 5

Related: How to Complete the FizzBuzz Challenge in 5 Programming Languages

Java Program to Find the Largest and Smallest Digit of a Number

Below is the Java program to find the largest and smallest digit of a number:

        // Java program to find the largest and smallest
// digit of a number

public class Main
{
    static void findLargestSmallest(int num)
    {
        int largestDigit = 0;
        int smallestDigit = 9;
        int digit;

        while(num != 0)
        {
            digit = num % 10;

            // Finding the largest digit
            largestDigit = Math.max(digit, largestDigit);

            // Finding the smallest digit
            smallestDigit = Math.min(digit, smallestDigit);

            num = num / 10;
        }
        System.out.println("Largest Digit: " + largestDigit);
        System.out.println("Smallest Digit: " + smallestDigit);
    }

    // Driver Code
    public static void main(String[] args) {
        int num1 = 238627;
        System.out.println("num1: " + num1);
        findLargestSmallest(num1);

        int num2 = 34552;
        System.out.println("num2: " + num2);
        findLargestSmallest(num2);

        int num3 = 123;
        System.out.println("num3: " + num3);
        findLargestSmallest(num3);

        int num4 = 45672;
        System.out.println("num4: " + num4);
        findLargestSmallest(num4);

        int num5 = 76567;
        System.out.println("num5: " + num5);
        findLargestSmallest(num5);
    }
}

Output:

        num1: 238627
Largest Digit: 8
Smallest Digit: 2
num2: 34552
Largest Digit: 5
Smallest Digit: 2
num3: 123
Largest Digit: 3
Smallest Digit: 1
num4: 45672
Largest Digit: 7
Smallest Digit: 2
num5: 76567
Largest Digit: 7
Smallest Digit: 5

Boost Your Python Skills Using Built-In Methods and Functions

Python Standard Library provides a number of built-in methods and functions that are used to perform a variety of tasks. Methods and functions increase the code clarity and efficiency. Leverage the power of methods and functions to boost up your Python skills.