# How to Find the Largest and Smallest Digits of a Number With Programming

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 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 Codeint 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: 238627Largest Digit: 8Smallest Digit: 2num2: 34552Largest Digit: 5Smallest Digit: 2num3: 123Largest Digit: 3Smallest Digit: 1num4: 45672Largest Digit: 7Smallest Digit: 2num5: 76567Largest Digit: 7Smallest Digit: 5``

## 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 numberdef 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 Codenum1 = 238627print("num1:", num1)findLargestSmallest(num1)num2 = 34552print("num2:", num2)findLargestSmallest(num2)num3 = 123print("num3:", num3)findLargestSmallest(num3)num4 = 45672print("num4:", num4)findLargestSmallest(num4)num5 = 76567print("num5:", num5)findLargestSmallest(num5)``

Output:

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

## 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 numberfunction 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 Codevar 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: 238627Largest Digit: 8Smallest Digit: 2num2: 34552Largest Digit: 5Smallest Digit: 2num3: 123Largest Digit: 3Smallest Digit: 1num4: 45672Largest Digit: 7Smallest Digit: 2num5: 76567Largest Digit: 7Smallest Digit: 5``

## 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 #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 Codeint 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: 238627Largest Digit: 8Smallest Digit: 2num2: 34552Largest Digit: 5Smallest Digit: 2num3: 123Largest Digit: 3Smallest Digit: 1num4: 45672Largest Digit: 7Smallest Digit: 2num5: 76567Largest Digit: 7Smallest Digit: 5``

## 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 numberpublic 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: 238627Largest Digit: 8Smallest Digit: 2num2: 34552Largest Digit: 5Smallest Digit: 2num3: 123Largest Digit: 3Smallest Digit: 1num4: 45672Largest Digit: 7Smallest Digit: 2num5: 76567Largest Digit: 7Smallest 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.

Author: Yuvraj Chandra

Source: Yuvraj Chandra.” How to Find the Largest and Smallest Digits of a Number With Programming”. Retrieved From https://www.makeuseof.com/find-the-largest-and-smallest-digit-of-a-number/