Largest and Smallest N-digit Hexadecimal Numbers
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: 1000Input: 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 Systemstring 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 Systemstring 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 numbervoid print(int largest){ cout << "Largest: " << findLargest(largest) << endl; cout << "Smallest: " << findSmallest(largest) << endl;}// Driver codeint main(){ int N = 4; print(N); return 0;} |
Java
// Java program to find the largest// and smallest N-digit numbers// in Hexa-Decimal Number Systemclass 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 Systemdef findLargest(N) : # Append 'F' N times largest = 'F'*N return largest;# Function to return the smallest# N-digit number in Hexa-Decimal# Number Systemdef 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 numberdef printAns(largest) : print("Largest: " , findLargest(largest)); print("Smallest: " , findSmallest(largest));# Driver codeif __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 Systemusing 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 Systemfunction 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 Systemfunction 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 numberfunction print(largest){ document.write("Largest: " + findLargest(largest) + "<br>"); document.write("Smallest: " + findSmallest(largest) + "<br>");}// Driver codevar N = 4;print(N);// This code is contributed by Shivanisingh</script> |
Largest: FFFF Smallest: 1000
Time Complexity: O(N) where N is the length of the string.
Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the Demo Class for First Step to Coding Course, specifically designed for students of class 8 to 12.
The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.



