Question: Implement Multiplication public class Main { public static void main(String[] args) { BigInt a = new BigInt(99); BigInt b = new BigInt(9); System.out.println(a +

Implement Multiplication

public class Main {

public static void main(String[] args)

{

BigInt a = new BigInt("99");

BigInt b = new BigInt("9");

System.out.println(a + " + " + b + " = " + a.add(b));

System.out.println(a + " - " + b + " = " + a.sub(b));

System.out.println(a + " * " + b + " = " + a.mul(b));

}

}

class BigInt

{

public BigInt()

{

num = new int[1];

}

public BigInt(String s)

{

num = new int[s.length()];

for (int i = 0; i < num.length; ++i)

{

num[num.length - i - 1] = s.charAt(i) - '0';

}

num = trim(num);

}

private BigInt(int[] n)

{

this.num = new int[n.length];

for (int i = 0; i < n.length; ++i)

{

this.num[i] = n[i];

}

}

public BigInt add(BigInt o)

{

int carry = 0;

int max = num.length > o.num.length ? num.length : o.num.length;

int[] result = new int[max+1];

for (int i = 0; i <= max; ++i)

{

int top = i < num.length ? num[i] : 0;

int bot = i < o.num.length ? o.num[i] : 0;

result[i] = (top + bot + carry) % 10;

carry = (top + bot + carry) / 10;

}

return new BigInt(trim(result));

}

public BigInt sub(BigInt o)

{

int borrow =10;

int max = num.length > o.num.length ? num.length : o.num.length;

int[] result = new int[max];

int x = 0;

for (int i = 0; i < max; ++i)

{

int top = i < num.length ? num[i] : 0;

int bot = i < o.num.length ? o.num[i] : 0;

if (top < bot)

if(x > 0)

{

result[i] = ((((top - x)+borrow))-bot);

x = 0;

x += 1;

}

else

{

result[i]= (top - bot) + borrow;

num[i+1] -= 1;

}

if( top == bot || top > bot)

result[i] = (top - bot) % 10;

}

return new BigInt(trim(result));

}

public BigInt mul(BigInt o)

{

{

//Code goes here

}

return null;

}

public String toString()

{

String s = "";

for (int i : num)

{

s = i + s;

}

return s;

}

private int[] trim(int[] nums)

{

int size = nums.length;

for (int i = nums.length - 1; i > 0; --i)

{

if (nums[i] != 0)

{

break;

}

--size;

}

int[] res = new int[size];

for (int i = 0; i < size; ++i)

{

res[i] = nums[i];

}

return res;

}

private int[] num;

}

I have addition and subtraction worked out can someone help me with multiplication part. Please provide output. Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!