Related Articles

• Last Updated : 16 Apr, 2021

Given a range [L, R]. The task is to find the total number of Hexadecimal alphabets that are required to write every number in the range.
Hexadecimal alphabets are the alphabets in the range [A, F] which are required to represent decimal numbers from the range [10, 15]
Examples:

Input: L = 10, R = 15
Output:
All the numbers from 10 to 15 contain a hexadecimal alphabet.
Input: L = 15, R = 16
Output:
15 and 16 are represented in hexadecimal as F and 10 respectively.

Approach:

1. First of all, check if num ≥ 10 and num ≤ 15. If yes then increment the count as decimal numbers from 10 to 15 contain a hexadecimal alphabet.
2. If num > 15 than update the number as num = num % 16. If it is greater than 10 than increment the count.
3. Repeat 2nd step till number (for every number) is greater than 0.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that will count``// total hexadecimal alphabet``int` `countHexadecimal(``int` `L, ``int` `R)``{``    ``int` `count = 0;``    ``for` `(``int` `i = L; i <= R; i++) {` `        ``// All the numbers from 10 to 15``        ``// contain a hexadecimal alphabet``        ``if` `(i >= 10 && i <= 15)``            ``count++;` `        ``// If i > 15 then perform mod by 16 repeatedly``        ``// till the number is > 0``        ``// If number % 16 > 10 then increase count``        ``else` `if` `(i > 15) {``            ``int` `k = i;``            ``while` `(k != 0) {``                ``if` `(k % 16 >= 10)``                    ``count++;``                ``k = k / 16;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``int` `main()``{``    ``int` `L = 5, R = 100;``    ``cout << countHexadecimal(L, R);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `// Function that will count``// total hexadecimal alphabet``static` `int` `countHexadecimal(``int` `L, ``int` `R)``{``    ``int` `count = ``0``;``    ``for` `(``int` `i = L; i <= R; i++)``    ``{` `        ``// All the numbers from 10 to 15``        ``// contain a hexadecimal alphabet``        ``if` `(i >= ``10` `&& i <= ``15``)``            ``count++;` `        ``// If i > 15 then perform mod by 16``        ``// repeatedly till the number is > 0``        ``// If number % 16 > 10 then increase count``        ``else` `if` `(i > ``15``)``        ``{``            ``int` `k = i;``            ``while` `(k != ``0``)``            ``{``                ``if` `(k % ``16` `>= ``10``)``                    ``count++;``                ``k = k / ``16``;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``public` `static` `void` `main(String args[])``{``    ``int` `L = ``5``, R = ``100``;``    ``System.out.print(countHexadecimal(L, R));``}``}` `// This code is contributed``// by Akanksha Rai`

## Python3

 `# Python3 implementation of the approach` `# Function that will count``# total hexadecimal alphabet``def` `countHexadecimal(L, R) :``    ``count ``=` `0``;``    ``for` `i ``in` `range``(L, R ``+` `1``) :` `        ``# All the numbers from 10 to 15``        ``# contain a hexadecimal alphabet``        ``if` `(i >``=` `10` `and` `i <``=` `15``) :``            ``count ``+``=` `1``;` `        ``# If i > 15 then perform mod by 16``        ``# repeatedly till the number is > 0``        ``# If number % 16 > 10 then``        ``# increase count``        ``elif` `(i > ``15``) :``            ``k ``=` `i;``            ``while` `(k !``=` `0``) :``                ``if` `(k ``%` `16` `>``=` `10``) :``                    ``count ``+``=` `1``;``                ``k ``=` `k ``/``/` `16``;` `    ``return` `count;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:``    ``L ``=` `5``; R ``=` `100``;``    ` `    ``print``(countHexadecimal(L, R));` `# This code is contributed by Ryuga`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ` `// Function that will count``// total hexadecimal alphabet``static` `int` `countHexadecimal(``int` `L, ``int` `R)``{``    ``int` `count = 0;``    ``for` `(``int` `i = L; i <= R; i++)``    ``{` `        ``// All the numbers from 10 to 15``        ``// contain a hexadecimal alphabet``        ``if` `(i >= 10 && i <= 15)``            ``count++;` `        ``// If i > 15 then perform mod by 16 repeatedly``        ``// till the number is > 0``        ``// If number % 16 > 10 then increase count``        ``else` `if` `(i > 15)``        ``{``            ``int` `k = i;``            ``while` `(k != 0)``            ``{``                ``if` `(k % 16 >= 10)``                    ``count++;``                ``k = k / 16;``            ``}``        ``}``    ``}` `    ``return` `count;``}` `// Driver code``public` `static` `void` `Main()``{``    ``int` `L = 5, R = 100;``    ``Console.Write(countHexadecimal(L, R));``}``}` `// This code is contributed``// by Akanksha Rai`

## PHP

 `= 10 && ``\$i` `<= 15)``            ``\$count``++;` `        ``// If i > 15 then perform mod by 16``        ``// repeatedly till the number is > 0``        ``// If number % 16 > 10 then increase count``        ``else` `if` `(``\$i` `> 15)``        ``{``            ``\$k` `= ``\$i``;``            ``while` `(``\$k` `!= 0)``            ``{``                ``if` `(``\$k` `% 16 >= 10)``                    ``\$count``++;``                ``\$k` `= ``\$k` `/ 16;``            ``}``        ``}``    ``}` `    ``return` `\$count``;``}` `// Driver code``\$L` `= 5;``\$R` `= 100;``echo` `countHexadecimal(``\$L``, ``\$R``);` `// This code is contributed by Ita_c``?>`

## Javascript

 ``
Output:
`36`

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up