Modulus of two Hexadecimal Numbers | Java program to implement the above approach ; Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
import java . util . * ; public class Main { static void hexaModK ( String N , String k ) { HashMap < Character , Integer > map = new HashMap < > ( ) ; for ( char i = '0' ; i <= '9' ; i ++ ) { map . put ( i , i - '0' ) ; } map . put ( ' A ' , 10 ) ; map . put ( ' B ' , 11 ) ; map . put ( ' C ' , 12 ) ; map . put ( ' D ' , 13 ) ; map . put ( ' E ' , 14 ) ; map . put ( ' F ' , 15 ) ; long m = Long . parseLong ( k , 16 ) ; long base = 1 ; long ans = 0 ; for ( int i = N . length ( ) - 1 ; i >= 0 ; i -- ) { long n = map . get ( N . charAt ( i ) ) % m ; ans = ( ans + ( base % m * n % m ) % m ) % m ; base = ( base % m * 16 % m ) % m ; } System . out . println ( Long . toHexString ( ans ) . toUpperCase ( ) ) ; } public static void main ( String args [ ] ) { String n = "3E8" ; String k = "13" ; hexaModK ( n , k ) ; } }