Multiply two numbers of different base and represent product in another given base
Given two numbers N, M in the bases X, Y and another base P. The task is to find the product of N and M and represent the product in base P.
Examples:
Input: N = 101, M = 110, X = 2, Y = 2, P = 16
Output:1E
Explanation: NX * MY = (101)2 * (110)2 = (11110)2
(11110)2 = (1E)16Input: N = 101, M = A, X = 2, Y = 20, P = 16
Output: 32
Explanation: Nx = (101)2 = (5)20
NX * MY = (5)20 * (A)20 = (2A)20
(2A)20 = (32)16
Approach: The approach is to convert the given numbers in decimal, perform the product and then turn it back to a number of base p. Follow the steps mentioned below:
- Convert NX and MY to decimal number.
- Perform multiplication on the decimal numbers.
- Convert the result of multiplication from decimal to base P.
Below is the implementation of the above approach.
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Convert Number from a given base // to decimal // Return the value of a char. static int value( char c) { if (c >= '0' && c <= '9' ) return ( int )c - '0' ; else return ( int )c - 'A' + 10; } // Function to convert a // number from given base to decimal int toDecimal(string s, int base) { int length = s.length(); // Initialize power of base and result int power = 1, ans = 0; // Decimal equivalent of s for ( int i = length - 1; i >= 0; i--) { ans += value(s[i]) * power; power = power * base; } return ans; } // Function to convert decimal // to any given base char reverseValue( int n) { if (n >= 0 && n <= 9) return ( char )(n + 48); else return ( char )(n - 10 + 65); } // Function to convert a given // decimal number to a base 'base' string toBase( int base, int num) { string s = "" ; // Convert input number is given // base by repeatedly dividing it // by base and taking remainder while (num > 0) { s += reverseValue(num % base); num /= base; } string sb = "" ; // Append a string into StringBuilder sb += (s); // Reverse the result reverse(sb.begin(), sb.end()); return sb; } // Function to find // the product of N and M void findProduct(string N, int X, string M, int Y, int P) { // Convert N to decimal int decimalX = toDecimal(N, X); // Convert y to decimal int decimalY = toDecimal(M, Y); // Multiply the decimal numbers int product = decimalX * decimalY; // Convert product to base string result = toBase(P, product); // Print the result cout << (result); } // Driver code int main() { string N = "101" , M = "110" ; int X = 2, Y = 2, P = 16; findProduct(N, X, M, Y, P); return 0; } // This code is contributed by Potta Lokesh |
Java
// Java code to implement above approach import java.io.*; class GFG { // Function to find // the product of N and M static void findProduct(String N, int X, String M, int Y, int P) { // Convert N to decimal int decimalX = toDecimal(N, X); // Convert y to decimal int decimalY = toDecimal(M, Y); // Multiply the decimal numbers int product = decimalX * decimalY; // Convert product to base String result = toBase(P, product); // Print the result System.out.println(result); } // Convert Number from a given base // to decimal // Return the value of a char. static int value( char c) { if (c >= '0' && c <= '9' ) return ( int )c - '0' ; else return ( int )c - 'A' + 10 ; } // Function to convert a // number from given base to decimal static int toDecimal(String s, int base) { int length = s.length(); // Initialize power of base and result int power = 1 , ans = 0 ; // Decimal equivalent of s for ( int i = length - 1 ; i >= 0 ; i--) { ans += value(s.charAt(i)) * power; power = power * base; } return ans; } // Function to convert decimal // to any given base static char reverseValue( int n) { if (n >= 0 && n <= 9 ) return ( char )(n + 48 ); else return ( char )(n - 10 + 65 ); } // Function to convert a given // decimal number to a base 'base' static String toBase( int base, int num) { String s = "" ; // Convert input number is given // base by repeatedly dividing it // by base and taking remainder while (num > 0 ) { s += reverseValue(num % base); num /= base; } StringBuilder sb = new StringBuilder(); // Append a string into StringBuilder sb.append(s); // Reverse the result return new String(sb.reverse()); } // Driver code public static void main(String[] args) { String N = "101" , M = "110" ; int X = 2 , Y = 2 , P = 16 ; findProduct(N, X, M, Y, P); } } |
C#
// C# code to implement above approach using System; class GFG { // Function to find // the product of N and M static void findProduct(String N, int X, String M, int Y, int P) { // Convert N to decimal int decimalX = toDecimal(N, X); // Convert y to decimal int decimalY = toDecimal(M, Y); // Multiply the decimal numbers int product = decimalX * decimalY; // Convert product to base String result = toBase(P, product); // Print the result Console.Write(result); } // Convert Number from a given base // to decimal // Return the value of a char. static int value( char c) { if (c >= '0' && c <= '9' ) return ( int )c - '0' ; else return ( int )c - 'A' + 10; } // Function to convert a // number from given base to decimal static int toDecimal(String s, int _base) { int length = s.Length; // Initialize power of base and result int power = 1, ans = 0; // Decimal equivalent of s for ( int i = length - 1; i >= 0; i--) { ans += value(s[i]) * power; power = power * _base; } return ans; } // Function to convert decimal // to any given base static char reverseValue( int n) { if (n >= 0 && n <= 9) return ( char )(n + 48); else return ( char )(n - 10 + 65); } // Function to convert a given // decimal number to a base 'base' static String toBase( int _base, int num) { String s = "" ; // Convert input number is given // base by repeatedly dividing it // by base and taking remainder while (num > 0) { s += reverseValue(num % _base); num /= _base; } String sb = "" ; // Append a string into StringBuilder sb += s; // Reverse the result char [] arr = sb.ToCharArray(); Array.Reverse(arr); return new string (arr); } // Driver code public static void Main() { String N = "101" , M = "110" ; int X = 2, Y = 2, P = 16; findProduct(N, X, M, Y, P); } } // This code is contributed by saurabh_jaiswal. |
1E
Time Complexity: O(D) where D is the maximum number of digits in N, M and product
Auxiliary Space: O(1)