# Thread: How to multiply and Divide Huge int numbers

1. ## How to multiply and Divide Huge int numbers

I need some help on a program for school that I am supposed to be able to input a integer number of unlimited size and mulitply it with another as well as divide,subtract, and add them. I have the add and subtract working. I read them as a string then convert each digit to a interger stored in a vector array and work it a digit at a time for the add and subtract. It works although its a bit awkward. I have no clue on the multiply and divide. Help before I loose all my hair.

thanks

2. i remember i made that exercise a long time a go i cant help you a lot but will try to give you the basics of the multiply and division how i did it.

as you said you have no problems with the add, the rest of operations reduce to the add operations ie:

to multiply 2 numbers:
4*8 = 32
the result is the sum of the first number until you get the second number
or the sum of 8 times 4
4+4+4+4+4+4+4+4 = 32

to divide 2 numbers:
is the inverse of the multiply
8/2 = 4

2+2+2+2 = 8 as you see you have "4" "2's" and 4 is the answer

ok, as i told you i made that exercise a long time a go and cant remember how i did it or where's the source code, but thats the "key" i found to solve that problem .
will try to find it, should be somewere

please excuse my poor english and good luck

3. You can also use basic elementary school multiplication and division methods that go one digit at a time.

4. you can implement the multiply and divide using addition and substraction which you had implemented like this:
code:
Code:
```class longInteger {
public:
.........
longInteger operator+(const longInteger& oprand);
longInteger operator+(int opint);
longInteger operator-(longInteger& oprand);
longInteger operator*(longInteger& oprand);
longInteger operator/(longInteger& oprand);
........
private:
......

};

longInteger longInteger::operator*(longInteger& oprand) {
longInteger result(*this),i;
for(i=0;i<oprand;i=i+1)
result = result + *this
}
return result
}

longInteger longInteger::operator/(longInteger& oprand) {
longInteger result(0), val(*this);

while((val = (val - oprand)) >0) {
result = result +1;
}
return result
}```

also you have to implement the operator>, constructor,copy constructor, operator=, ...
I just give you a algorithm, my code might not work cuz I haven't had it tested.