Given two positive numbers as strings. The numbers may be very large (may not fit in long long int), the task is to find product of these two numbers.
Input : num1 = 4154 num2 = 51454 Output : 213739916 Input : num1 = 654154154151454545415415454 num2 = 63516561563156316545145146514654 Output : 41549622603955309777243716069997997007620439937711509062916
We start from last digit of second number multiply it with first number. Then we multiply second digit of second number with first number, and so on. We add all these multiplications. While adding, we put i-th multiplication shifted.
The approach used in below solution is to keep only one array for result. We traverse all digits first and second numbers in a loop and add the result at appropriate position.
The above code is adapted from the code provided by Gaurav.
Time Complexity: O(m*n), where m and n are length of two number that need to be multiplied.
Related Article :
Karatsuba algorithm for fast multiplication
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Multiply large integers under large modulo
- Modulo power for large numbers represented as strings
- Multiply Large Numbers using Grid Method
- Divide large number represented as string
- Square of large number represented as String
- Multiply N complex numbers given as strings
- Russian Peasant (Multiply two numbers using bitwise operators)
- Subtract Two Numbers represented as Linked Lists
- Check if given number can be represented as sum of two great numbers
- Add two numbers represented by two arrays
- Count numbers which can be represented as sum of same parity primes
- Add two numbers represented by Stacks
- Check if a number can be represented as a sum of 2 triangular numbers
- Sum of two numbers where one number is represented as array of digits
- Compare numbers represented by Linked Lists
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Efficient way to multiply with 7
- Multiply two integers without using multiplication, division and bitwise operators, and no loops