Posted on by Kalkicode
Code String

# Multiply two string numbers

Here given code implementation process.

``````// Java program
// Multiply two string numbers
class MyString
{
//Add two string numbers
public String addition(String str1, String str2)
{
//Get the last location
int size1 = str1.length() - 1;
int size2 = str2.length() - 1;
//Use to store the result
String result = "";
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = str1.charAt(size1) - '0';
}
if (size2 >= 0)
{
//Get the second number
num2 = str2.charAt(size2) - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
if (carry != 0)
{
carry /= 10;
}
//get next location
//modified string text location
size1--;
size2--;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
public void mutiply(String str1, String str2)
{
//Get the size of numeric string
int size1 = str1.length() - 1;
int size2 = str2.length() - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
String result = "";
String temp = "";
String zeros = "";
//arithmetic operation variable
int num1 = 0;
int num2 = 0;
long carry = 0;
//Loop contolling variables
int i = 0, k = 0;
int num1_negative = 0;
int num2_negative = 0;
if (str1.charAt(0) == '-')
{
num1_negative = 1;
}
if (str2.charAt(0) == '-')
{
num2_negative = 1;
}
for (i = size2; i >= num2_negative; i--)
{
k = size1;
temp = "";
num1 = str2.charAt(i) - '0';
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = str1.charAt(k) - '0';
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = carry % 10 + temp;
if (carry != 0)
{
carry = carry / 10;
}
//modified string text location
k--;
}
temp = temp + zeros;
result = addition(temp, result);
zeros += "0";
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
//When one number is nagative
System.out.print("\n(" + str1 + ") * (" + str2 + ") = -" + result);
}
else
{
System.out.print("\n(" + str1 + ") * (" + str2 + ") = " + result);
}
}
public static void main(String[] args)
{
MyString obj = new MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
}
}``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````//Include header file
#include <iostream>
#include<string.h>

using namespace std;

// C++ program
// Multiply two string numbers
class MyString
{
public:
//Add two string numbers
string addition(string str1, string str2)
{
//Get the last location
int size1 = str1.size() - 1;
int size2 = str2.size() - 1;
//Use to store the result
string result = "";
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = str1[size1] - '0';
}
if (size2 >= 0)
{
//Get the second number
num2 = str2[size2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = to_string(carry % 10) + result;
if (carry != 0)
{
carry /= 10;
}
//get next location
//modified string text location
size1--;
size2--;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
void mutiply(string str1, string str2)
{
//Get the size of numeric string
int size1 = str1.size() - 1;
int size2 = str2.size() - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
string result = "";
string temp = "";
string zeros = "";
//arithmetic operation variable
int num1 = 0;
int num2 = 0;
long carry = 0;
//Loop contolling variables
int i = 0, k = 0;
int num1_negative = 0;
int num2_negative = 0;
if (str1[0] == '-')
{
num1_negative = 1;
}
if (str2[0] == '-')
{
num2_negative = 1;
}
for (i = size2; i >= num2_negative; i--)
{
k = size1;
temp = "";
num1 = str2[i] - '0';
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = str1[k] - '0';
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = to_string(carry % 10) + temp;
if (carry != 0)
{
carry = carry / 10;
}
//modified string text location
k--;
}
temp = temp + zeros;
result = this->addition(temp, result);
zeros += "0";
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
//When one number is nagative
cout << "\n(" << str1 << ") * (" << str2 << ") = -" << result;
}
else
{
cout << "\n(" << str1 << ") * (" << str2 << ") = " << result;
}
}
};
int main()
{
MyString obj = MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
return 0;
}``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````//Include namespace system
using System;
// C# program
// Multiply two string numbers
class MyString
{
//Add two string numbers
public String addition(String str1, String str2)
{
//Get the last location
int size1 = str1.Length - 1;
int size2 = str2.Length - 1;
//Use to store the result
String result = "";
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = str1[size1] - '0';
}
if (size2 >= 0)
{
//Get the second number
num2 = str2[size2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
if (carry != 0)
{
carry /= 10;
}
//get next location
//modified string text location
size1--;
size2--;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
public void mutiply(String str1, String str2)
{
//Get the size of numeric string
int size1 = str1.Length - 1;
int size2 = str2.Length - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
String result = "";
String temp = "";
String zeros = "";
//arithmetic operation variable
int num1 = 0;
int num2 = 0;
long carry = 0;
//Loop contolling variables
int i = 0, k = 0;
int num1_negative = 0;
int num2_negative = 0;
if (str1[0] == '-')
{
num1_negative = 1;
}
if (str2[0] == '-')
{
num2_negative = 1;
}
for (i = size2; i >= num2_negative; i--)
{
k = size1;
temp = "";
num1 = str2[i] - '0';
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = str1[k] - '0';
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = carry % 10 + temp;
if (carry != 0)
{
carry = carry / 10;
}
//modified string text location
k--;
}
temp = temp + zeros;
result = addition(temp, result);
zeros += "0";
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
//When one number is nagative
Console.Write("\n(" + str1 + ") * (" + str2 + ") = -" + result);
}
else
{
Console.Write("\n(" + str1 + ") * (" + str2 + ") = " + result);
}
}
public static void Main(String[] args)
{
MyString obj = new MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
}
}``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````<?php
// Php program
// Multiply two string numbers
class MyString
{
//Add two string numbers
public  function addition(\$str1, \$str2)
{
//Get the size
\$size1 = strlen(\$str1) - 1;
\$size2 = strlen(\$str2) - 1;
//Use to store the result
\$result = "";
\$num1 = 0;
\$num2 = 0;
\$carry = 0;
//Execute loop to get both string number
while (\$size1 >= 0 || \$size2 >= 0 || \$carry != 0)
{
\$num1 = 0;
\$num2 = 0;
if (\$size1 >= 0)
{
//Get the first number
\$num1 = ord(\$str1[\$size1]) - ord('0');
}
if (\$size2 >= 0)
{
//Get the second number
\$num2 = ord(\$str2[\$size2]) - ord('0');
}
\$carry = \$carry + \$num1 + \$num2;
//add sum into the result
\$result = (\$carry % 10) . \$result;
if (\$carry != 0)
{
\$carry = intval(\$carry / 10);
}
//get next location
//modified string text location
\$size1--;
\$size2--;
}

return \$result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
public  function mutiply(\$str1, \$str2)
{
//Get the size of numeric string
\$size1 = strlen(\$str1) - 1;
\$size2 = strlen(\$str2) - 1;
if (\$size1 < 0 || \$size2 < 0)
{
return;
}
//Use to store the result
\$result = "";
\$temp = "";
\$zeros = "";
//arithmetic operation variable
\$num1 = 0;
\$num2 = 0;
\$carry = 0;
//Loop contolling variables
\$i = 0;
\$k = 0;
\$num1_negative = 0;
\$num2_negative = 0;
if (\$str1[0] == '-')
{
\$num1_negative = 1;
}
if (\$str2[0] == '-')
{
\$num2_negative = 1;
}
for (\$i = \$size2; \$i >= \$num2_negative; \$i--)
{
\$k = \$size1;
\$temp = "";
\$num1 = ord(\$str2[\$i]) - ord('0');
//multiply num1 into another string elements
while (\$k >= \$num1_negative || \$carry != 0)
{
if (\$k >= \$num1_negative)
{
//Get the second number
\$num2 = ord(\$str1[\$k]) - ord('0');
}
else
{
\$num2 = 0;
}
\$carry = \$carry + (\$num1 * \$num2);
//add last digit of carray
\$temp = (\$carry % 10) . \$temp;
if (\$carry != 0)
{
\$carry = intval(\$carry / 10);
}
//modified string text location
\$k--;
}
\$temp = \$temp . \$zeros;

\$result = \$this->addition(\$temp, \$result);

\$zeros = \$zeros . "0";
}
if ((\$num1_negative == 1 && \$num2_negative == 0) || (\$num1_negative == 0 && \$num2_negative == 1))
{
echo "\n(". \$str1 .") * (". \$str2 .") = -". \$result;
}
else
{
echo "\n(". \$str1 .") * (". \$str2 .") = ". \$result;
}
}
}

function main()
{
\$obj = new MyString();
//Some test case
\$obj->mutiply("14", "2");
\$obj->mutiply("3", "9");
//When one number is negative
\$obj->mutiply("-14", "2");
//When both number is negative
\$obj->mutiply("-2", "-2");
//When number is large
\$obj->mutiply("7564353234554", "3233243424212128534124678");
\$obj->mutiply("3233243424212128534124678", "7564353234554");
}
main();``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````// Node Js program
// Multiply two string numbers
class MyString
{
//Add two string numbers
{
//Get the last location
var size1 = str1.length - 1;
var size2 = str2.length - 1;
//Use to store the result
var result = "";
var num1 = 0;
var num2 = 0;
var carry = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = str1.charAt(size1) - '0';
}
if (size2 >= 0)
{
//Get the second number
num2 = str2.charAt(size2) - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
if (carry != 0)
{
carry = parseInt(carry / 10);
}
//get next location
//modified string text location
size1--;
size2--;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
mutiply(str1, str2)
{
//Get the size of numeric string
var size1 = str1.length - 1;
var size2 = str2.length - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
var result = "";
var temp = "";
var zeros = "";
//arithmetic operation variable
var num1 = 0;
var num2 = 0;
var carry = 0;
//Loop contolling variables
var i = 0;
var k = 0;
var num1_negative = 0;
var num2_negative = 0;
if (str1.charAt(0) == '-')
{
num1_negative = 1;
}
if (str2.charAt(0) == '-')
{
num2_negative = 1;
}
for (i = size2; i >= num2_negative; i--)
{
k = size1;
temp = "";
num1 = str2.charAt(i) - '0';
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = str1.charAt(k) - '0';
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = carry % 10 + temp;
if (carry != 0)
{
carry = parseInt(carry / 10);
}
//modified string text location
k--;
}
temp = temp + zeros;
result = this.addition(temp, result);
zeros += "0";
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
process.stdout.write("\n(" + str1 + ") * (" + str2 + ") = -" + result);
}
else
{
process.stdout.write("\n(" + str1 + ") * (" + str2 + ") = " + result);
}
}
}

function main()
{
var obj = new MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
}
main();``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````#  Python 3 program
#  Multiply two string numbers
class MyString :
# Add two string numbers
def addition(self, str1, str2) :
# Get the last location
size1 = len(str1) - 1
size2 = len(str2) - 1
# Use to store the result
result = ""
num1 = 0
num2 = 0
carry = 0
# Execute loop to get both string number
while (size1 >= 0 or size2 >= 0 or carry != 0) :
num1 = 0
num2 = 0
if (size1 >= 0) :
# Get the first number
num1 = ord(str1[size1]) - ord('0')

if (size2 >= 0) :
# Get the second number
num2 = ord(str2[size2]) - ord('0')

carry = carry + num1 + num2
# add sum into the result
result = str(carry % 10) + result
if (carry != 0) :
carry = int(carry / 10)

# get next location
# modified string text location
size1 -= 1
size2 -= 1

return result

# mutiply two string numbers
# Assuming that given both strings is valid numbers
def mutiply(self, str1, str2) :
# Get the size of numeric string
size1 = len(str1) - 1
size2 = len(str2) - 1
if (size1 < 0 or size2 < 0) :
return

# Use to store the result
result = ""
temp = ""
zeros = ""
# arithmetic operation variable
num1 = 0
num2 = 0
carry = 0
# Loop contolling variables
i = 0
k = 0
num1_negative = 0
num2_negative = 0
if (str1[0] == '-') :
num1_negative = 1

if (str2[0] == '-') :
num2_negative = 1

i = size2
while (i >= num2_negative) :
k = size1
temp = ""
num1 = ord(str2[i]) - ord('0')
# multiply num1 into another string elements
while (k >= num1_negative or carry != 0) :
if (k >= num1_negative) :
# Get the second number
num2 = ord(str1[k]) - ord('0')
else :
num2 = 0

carry = carry + (num1 * num2)
# add last digit of carray
temp = str(carry % 10) + temp
if (carry != 0) :
carry = int(carry / 10)

# modified string text location
k -= 1

temp = temp + zeros
result = self.addition(temp, result)
zeros += "0"
i -= 1

if ((num1_negative == 1 and num2_negative == 0) or(num1_negative == 0 and num2_negative == 1)) :
print("\n(", str1 ,") * (", str2 ,") = -", result, end = "")
else :
print("\n(", str1 ,") * (", str2 ,") = ", result, end = "")

def main() :
obj = MyString()
# Some test case
obj.mutiply("14", "2")
obj.mutiply("3", "9")
# When one number is negative
obj.mutiply("-14", "2")
# When both number is negative
obj.mutiply("-2", "-2")
# When number is large
obj.mutiply("7564353234554", "3233243424212128534124678")
obj.mutiply("3233243424212128534124678", "7564353234554")

if __name__ == "__main__": main()``````

#### Output

``````( 14 ) * ( 2 ) =  28
( 3 ) * ( 9 ) =  27
( -14 ) * ( 2 ) = - 28
( -2 ) * ( -2 ) =  4
( 7564353234554 ) * ( 3233243424212128534124678 ) =  24457395354039465236143206596413723612
( 3233243424212128534124678 ) * ( 7564353234554 ) =  24457395354039465236143206596413723612``````
``````#  Ruby program
#  Multiply two string numbers
class MyString

# Add two string numbers

# Get the last location
size1 = str1.length() - 1
size2 = str2.length() - 1
# Use to store the result
result = ""
num1 = 0
num2 = 0
carry = 0
# Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)

num1 = 0
num2 = 0
if (size1 >= 0)

# Get the first number
num1 = (str1[size1]).ord - ('0').ord
end
if (size2 >= 0)

# Get the second number
num2 = (str2[size2]).ord - ('0').ord
end
carry = carry + num1 + num2
# add sum into the result
result = (carry % 10).to_s + result
if (carry != 0)

carry = carry / 10
end
# get next location
# modified string text location
size1 -= 1
size2 -= 1
end
return result
end
# mutiply two string numbers
# Assuming that given both strings is valid numbers
def mutiply(str1, str2)

# Get the size of numeric string
size1 = str1.length() - 1
size2 = str2.length() - 1
if (size1 < 0 || size2 < 0)

return
end
# Use to store the result
result = ""
temp = ""
zeros = ""
# arithmetic operation variable
num1 = 0
num2 = 0
carry = 0
# Loop contolling variables
i = 0
k = 0
num1_negative = 0
num2_negative = 0
if (str1[0] == '-')

num1_negative = 1
end
if (str2[0] == '-')

num2_negative = 1
end
i = size2
while (i >= num2_negative)

k = size1
temp = ""
num1 = (str2[i]).ord - ('0').ord
# multiply num1 into another string elements
while (k >= num1_negative || carry != 0)

if (k >= num1_negative)

# Get the second number
num2 = (str1[k]).ord - ('0').ord
else

num2 = 0
end
carry = carry + (num1 * num2)
# add last digit of carray
temp = (carry % 10).to_s + temp
if (carry != 0)

carry = carry / 10
end
# modified string text location
k -= 1
end
temp = temp + zeros
result = self.addition(temp, result)
zeros += "0"
i -= 1
end
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))

# When one number is nagative
print("\n(", str1 ,") * (", str2 ,") = -", result)
else

print("\n(", str1 ,") * (", str2 ,") = ", result)
end
end
end
def main()

obj = MyString.new()
# Some test case
obj.mutiply("14", "2")
obj.mutiply("3", "9")
# When one number is negative
obj.mutiply("-14", "2")
# When both number is negative
obj.mutiply("-2", "-2")
# When number is large
obj.mutiply("7564353234554", "3233243424212128534124678")
obj.mutiply("3233243424212128534124678", "7564353234554")
end
main()``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````// Scala program
// Multiply two string numbers
class MyString
{
//Add two string numbers
def addition(str1: String, str2: String): String = {
//Get the last location
var size1: Int = str1.length() - 1;
var size2: Int = str2.length() - 1;
//Use to store the result
var result: String = "";
var num1: Int = 0;
var num2: Int = 0;
var carry: Int = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = str1(size1) - '0';
}
if (size2 >= 0)
{
//Get the second number
num2 = str2(size2) - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = ""+(carry % 10) + result;
if (carry != 0)
{
carry = (carry / 10).toInt;
}
//get next location
//modified string text location
size1 -= 1;
size2 -= 1;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
def mutiply(str1: String, str2: String): Unit = {
//Get the size of numeric string
var size1: Int = str1.length() - 1;
var size2: Int = str2.length() - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
var result: String = "";
var temp: String = "";
var zeros: String = "";
//arithmetic operation variable
var num1: Int = 0;
var num2: Int = 0;
var carry: Long = 0;
//Loop contolling variables
var i: Int = 0;
var k: Int = 0;
var num1_negative: Int = 0;
var num2_negative: Int = 0;
if (str1(0) == '-')
{
num1_negative = 1;
}
if (str2(0) == '-')
{
num2_negative = 1;
}
i = size2;
while (i >= num2_negative)
{
k = size1;
temp = "";
num1 = str2(i) - '0';
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = str1(k) - '0';
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = ""+(carry % 10) + temp;
if (carry != 0)
{
carry = (carry / 10).toInt;
}
//modified string text location
k -= 1;
}
temp = temp + zeros;
result = addition(temp, result);
zeros += "0";
i -= 1;
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
//When one number is nagative
print("\n(" + str1 + ") * (" + str2 + ") = -" + result);
}
else
{
print("\n(" + str1 + ") * (" + str2 + ") = " + result);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
}
}``````

#### Output

``````(14) * (2) = 28
(3) * (9) = 27
(-14) * (2) = -28
(-2) * (-2) = 4
(7564353234554) * (3233243424212128534124678) = 24457395354039465236143206596413723612
(3233243424212128534124678) * (7564353234554) = 24457395354039465236143206596413723612``````
``````// Swift program
// Multiply two string numbers
class MyString
{
//Add two string numbers
func addition(_ text1: String, _ text2: String) -> String
{   let str1 = Array(text1);
let str2 = Array(text2);
//Get the last location
var size1: Int = str1.count - 1;
var size2: Int = str2.count - 1;
//Use to store the result
var result: String = "";
var num1: Int = 0;
var num2: Int = 0;
var carry: Int = 0;
//Execute loop to get both string number
while (size1 >= 0 || size2 >= 0 || carry != 0)
{
num1 = 0;
num2 = 0;
if (size1 >= 0)
{
//Get the first number
num1 = Int(UnicodeScalar(String(str1[size1]))!.value  - UnicodeScalar("0")!.value);
}
if (size2 >= 0)
{
//Get the second number
num2 = Int(UnicodeScalar(String(str2[size2]))!.value  - UnicodeScalar("0")!.value);
}
carry = carry + num1 + num2;
//add sum into the result
result = String(carry % 10) + result;
if (carry != 0)
{
carry = carry / 10;
}
//get next location
//modified string text location
size1 -= 1;
size2 -= 1;
}
return result;
}
//mutiply two string numbers
//Assuming that given both strings is valid numbers
func mutiply(_ text1: String, _ text2: String)
{   let str1 = Array(text1);
let str2 = Array(text2);
//Get the size of numeric string
let size1: Int = str1.count - 1;
let size2: Int = str2.count - 1;
if (size1 < 0 || size2 < 0)
{
return;
}
//Use to store the result
var result: String = "";
var temp: String = "";
var zeros: String = "";
//arithmetic operation variable
var num1: Int = 0;
var num2: Int = 0;
var carry: Int = 0;
//Loop contolling variables
var i: Int = 0;
var k: Int = 0;
var num1_negative: Int = 0;
var num2_negative: Int = 0;
if (str1[0] == "-")
{
num1_negative = 1;
}
if (str2[0] == "-")
{
num2_negative = 1;
}
i = size2;
while (i >= num2_negative)
{
k = size1;
temp = "";
num1 =  Int(UnicodeScalar(String(str2[i]))!.value  - UnicodeScalar("0")!.value);
//multiply num1 into another string elements
while (k >= num1_negative || carry != 0)
{
if (k >= num1_negative)
{
//Get the second number
num2 = Int(UnicodeScalar(String(str1[k]))!.value  - UnicodeScalar("0")!.value);
}
else
{
num2 = 0;
}
carry = carry + (num1 * num2);
//add last digit of carray
temp = String(carry % 10) + temp;
if (carry != 0)
{
carry = carry / 10;
}
//modified string text location
k -= 1;
}
temp = temp + zeros;
result = self.addition(temp, result);
zeros += "0";
i -= 1;
}
if ((num1_negative == 1 && num2_negative == 0) || (num1_negative == 0 && num2_negative == 1))
{
print("\n(", text1 ,") * (", text2 ,") = -", result, terminator: "");
}
else
{
print("\n(", text1 ,") * (", text2 ,") = ", result, terminator: "");
}
}
}
func main()
{
let obj: MyString = MyString();
//Some test case
obj.mutiply("14", "2");
obj.mutiply("3", "9");
//When one number is negative
obj.mutiply("-14", "2");
//When both number is negative
obj.mutiply("-2", "-2");
//When number is large
obj.mutiply("7564353234554", "3233243424212128534124678");
obj.mutiply("3233243424212128534124678", "7564353234554");
}
main();``````

#### Output

``````( 14 ) * ( 2 ) =  28
( 3 ) * ( 9 ) =  27
( -14 ) * ( 2 ) = - 28
( -2 ) * ( -2 ) =  4
( 7564353234554 ) * ( 3233243424212128534124678 ) =  24457395354039465236143206596413723612
( 3233243424212128534124678 ) * ( 7564353234554 ) =  24457395354039465236143206596413723612``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post