Skip to content
Related Articles

Related Articles

Improve Article

Largest and Smallest N-digit Hexadecimal Numbers

  • Last Updated : 19 May, 2021

Given an integer N, the task is to find the smallest and largest N-digit numbers Hexa-Decimal Number System.

Examples: 

Input: N = 4 
Output: 
Largest: FFFF 
Smallest: 1000

Input: N = 2 
Output: 
Largest: FF 
Smallest: 10 

Approach: The following steps can be followed to complete the required answer: 



  • Largest Number: To get the largest number, every digit of the number must be maximum. The maximum digit in the Hexa-Decimal number system is ‘F‘. Therefore: 
1 Digit Largest Number: 'F'
2 Digit Largest Number: 'FF'
3 Digit Largest Number: 'FFF'
                .
                .
                .
N Digit Largest Number: 'FFF....(N) times'
  • Smallest Number: The smallest number in hexadecimal numbers is ‘0‘. The idea is that the first digit needs to be as minimal as possible other than 0 which is ‘1’ and the remaining digits need to be 0. Therefore: 
1 Digit Smallest Number: '1'
2 Digit Smallest Number: '10'
3 Digit Smallest Number: '100'
                .
                .
                .
N Digit Smallest Number: '100....(N - 1) times'

Below is the implementation of the above approach: 

C++




// C++ program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
string findLargest(int N)
{
    // Append 'F' N times
    string largest = string(N, 'F');
 
    return largest;
}
 
// Function to return the smallest
// N-digit number in Hexa-Decimal
// Number System
string findSmallest(int N)
{
    // Append '0' (N - 1) times to 1
    string smallest = "1" + string((N - 1), '0');
 
    return smallest;
}
 
// Function to print the largest and smallest
// N-digit Hexa-Decimal number
void print(int largest)
{
    cout << "Largest: " << findLargest(largest) << endl;
    cout << "Smallest: " << findSmallest(largest) << endl;
}
 
// Driver code
int main()
{
 
    int N = 4;
    print(N);
 
    return 0;
}

Java




// Java program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
class GFG {
     
    // Function to return the largest
    // N-digit number in Hexa-Decimal
    // Number System
    static String findLargest(int N)
    {
         
        String largest = "";
        // Append 'F' N times
        for (int i = 0; i < N ; i++)
            largest += 'F';
     
        return largest;
    }
     
    // Function to return the smallest
    // N-digit number in Hexa-Decimal
    // Number System
    static String findSmallest(int N)
    {
         
        String smallest = "1" ;
        // Append '0' (N - 1) times to 1
        for(int i = 0; i < N - 1; i++)
            smallest += '0';
     
        return smallest;
    }
     
    // Function to print the largest and smallest
    // N-digit Hexa-Decimal number
    static void print(int largest)
    {
        System.out.println("Largest: " + findLargest(largest)) ;
        System.out.println("Smallest: " + findSmallest(largest)) ;
    }
     
    // Driver code
    public static void main (String[] args)
    {
     
        int N = 4;
        print(N);
     
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python3 program to find the largest
# and smallest N-digit numbers
# in Hexa-Decimal Number System
 
# Function to return the largest
# N-digit number in Hexa-Decimal
# Number System
def findLargest(N) :
 
    # Append 'F' N times
    largest = 'F'*N
 
    return largest;
 
# Function to return the smallest
# N-digit number in Hexa-Decimal
# Number System
def findSmallest(N) :
 
    # Append '0' (N - 1) times to 1
    smallest = '1' + '0'*(N - 1)
 
    return smallest;
 
# Function to print the largest and smallest
# N-digit Hexa-Decimal number
def printAns(largest) :
 
    print("Largest: " , findLargest(largest));
    print("Smallest: " , findSmallest(largest));
 
# Driver code
if __name__ == "__main__" :
 
    N = 4;
    printAns(N);
 
# This code is contributed by AnkitRai01

C#




// C# program to find the largest
// and smallest N-digit numbers
// in Hexa-Decimal Number System
using System;
 
class GFG {
     
    // Function to return the largest
    // N-digit number in Hexa-Decimal
    // Number System
    static string findLargest(int N)
    {
         
        string largest = "";
         
        // Append 'F' N times
        for (int i = 0; i < N ; i++)
            largest += 'F';
     
        return largest;
    }
     
    // Function to return the smallest
    // N-digit number in Hexa-Decimal
    // Number System
    static string findSmallest(int N)
    {
         
        string smallest = "1" ;
         
        // Append '0' (N - 1) times to 1
        for(int i = 0; i < N - 1; i++)
            smallest += '0';
     
        return smallest;
    }
     
    // Function to print the largest and smallest
    // N-digit Hexa-Decimal number
    static void print(int largest)
    {
        Console.WriteLine("Largest: " +
                    findLargest(largest)) ;
        Console.WriteLine("Smallest: " +
                    findSmallest(largest)) ;
    }
     
    // Driver code
    public static void Main(string[] args)
    {
        int N = 4;
        print(N);
    }
}
 
// This code is contributed by AnkitRai01

Javascript




<script>
// Javascipt implementation of the
// above approach
 
// Function to return the largest
// N-digit number in Hexa-Decimal
// Number System
function findLargest(N)
{
    // Append 'F' N times "a".repeat(10)
    var largest = "F".repeat(N);
   
    return largest;
}
   
// Function to return the smallest
// N-digit number in Hexa-Decimal
// Number System
function findSmallest(N)
{
    // Append '0' (N - 1) times to 1
    var smallest = "1" + "0".repeat(N-1);
   
    return smallest;
}
   
// Function to print the largest and smallest
// N-digit Hexa-Decimal number
function print(largest)
{
    document.write("Largest: " + findLargest(largest) + "<br>");
    document.write("Smallest: " + findSmallest(largest) + "<br>");
}
 
// Driver code
var N = 4;
print(N);
 
// This code is contributed by Shivanisingh
</script>
Output: 
Largest: FFFF
Smallest: 1000

 

Time Complexity: O(N) where N is the length of the string.
 

Attention reader! Don’t stop learning now. Participate in the Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :