
Dorsa R.
asked 08/28/20C programming :
please write a program to multiply and divide two numbers with 50 digits.
2 Answers By Expert Tutors

Patrick B. answered 08/29/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#define MAX_LEN (50)
#define BUFF_LEN ((2 * (MAX_LEN))+1)
void LongIntNum_MultiplyByDigit( char * longIntNum, char digit, char * product)
{
char partial_product;
char carry=0;
char Digit;
int iLoop;
int N = strlen(longIntNum);
printf(" strlen N = %d \n",N);
memset(product,0,BUFF_LEN);
for (iLoop=N-1; iLoop>=0; iLoop--)
{
partial_product = (longIntNum[iLoop]-48) * digit + carry;
printf(" partial product = %d \n",partial_product);
Digit = partial_product % 10;
carry = partial_product / 10;
printf(" Digit = %d : carry = %d \n",Digit,carry);
product[iLoop]=Digit+48;
}
product[iLoop]=carry+48;
}
LongIntNum_AddZeros(char * longIntNum, int num_zeros, char * product)
{
int N = strlen(longIntNum);
memset(product,0,BUFF_LEN);
memcpy(product,longIntNum,N);
char * cur = &product[N];
for (int iLoop=0; iLoop<num_zeros; iLoop++)
{
*cur = '0';
cur++;
}
}
LongIntNum_LeadingZeros(char * longIntNum,int num_zeros, char * LongIntNum)
{
memset(LongIntNum,0,BUFF_LEN);
char *cur = LongIntNum;
for (int iLoop=0; iLoop<num_zeros; iLoop++)
{
*cur='0';
cur++;
}
memcpy(cur,longIntNum,strlen(longIntNum));
}
void LongIntNum_Show(char * longIntNum,char * debugMsg)
{
if (debugMsg!=NULL)
{
printf("************************************************\n");
printf(debugMsg);
}
printf("************************************************\n");
int N = strlen(longIntNum);
printf(" Show:N= %d\n",N);
for (int iLoop=0; iLoop<N; iLoop++)
{
printf("%c",longIntNum[iLoop]);
}
printf("\n");
}
void LongIntNum_Add(char * longIntNum1, char * longIntNum2, char * longIntNumSum)
{
int N1 = strlen(longIntNum1);
int N2 = strlen(longIntNum2);
int N = -1;
int iLoop;
char LongIntNum1[BUFF_LEN];
char LongIntNum2[BUFF_LEN];
memset(LongIntNum1,0,BUFF_LEN);
memset(LongIntNum2,0,BUFF_LEN);
char * cur1;
char * cur2;
if (N1>=N2)
{
N=N1;
memcpy(LongIntNum1,longIntNum1,N1);
LongIntNum_LeadingZeros(longIntNum2,N1-N2,LongIntNum2);
cur1 = (&LongIntNum1[N-1]);
cur2 = (&LongIntNum2[N-1]);
}
else
{
N=N2;
memcpy(LongIntNum2,longIntNum2,N2);
LongIntNum_LeadingZeros(longIntNum1,N2-N1,LongIntNum1);
cur1 = (&LongIntNum2[N-1]);
cur2 = (&LongIntNum1[N-1]);
}
memset(longIntNumSum,0,BUFF_LEN);
char carry=0;
char Digit;
char curSum;
for (iLoop=N; iLoop>=1; iLoop--)
{
curSum = ((*cur1)-48) + (*(cur2)-48) + carry;
Digit = curSum % 10;
carry = curSum/ 10;
longIntNumSum[iLoop]=Digit+48;
cur1--; cur2--;
}
longIntNumSum[iLoop]=carry+48;
}
void LongIntNum_Multiply(char * longIntNum1, char * longIntNum2, char * longIntNumProduct)
{
char PartialProduct[BUFF_LEN];
char partialProduct[BUFF_LEN];
char runningTotal[BUFF_LEN];
char curDigit;
int N = strlen(longIntNum2);
memset(longIntNumProduct,0,BUFF_LEN);
for (int iLoop=N-1; iLoop>=0; iLoop--)
{
curDigit = longIntNum2[iLoop]-48;
int num_zeros = N-iLoop-1;
LongIntNum_MultiplyByDigit(longIntNum1,curDigit,partialProduct);
LongIntNum_Show(partialProduct,"Partial Product Pre-Pad");
LongIntNum_AddZeros(partialProduct,num_zeros,PartialProduct);
LongIntNum_Show(PartialProduct,"Partial Product");
memcpy(runningTotal,longIntNumProduct,BUFF_LEN);
LongIntNum_Add(PartialProduct,runningTotal,longIntNumProduct);
}
}
int main()
{
char longIntNum1[]="1234";
char longIntNum2[]="567";
char longIntNumProduct[BUFF_LEN];
LongIntNum_Multiply(longIntNum1,longIntNum2,longIntNumProduct);
LongIntNum_Show(longIntNumProduct,NULL);
}
Charlie M. answered 08/29/20
Friendly and Effective Electrical Engineer - Calc Algebra Trig Java C#
Hey Dorsa, I'd love to help you on this - There's a few ways you can accomplish this task in C without any extra libraries
It depends on how those two numbers are stored! But since numbers like this are often expressed as strings since strings can hold effectively an infinite number of digits, i'll assume that those numbers you need to multiply and divide are strings also.
Let's think about how we want to do this. The first thing that comes to mind is to treat these big numbers normally as though we're back in elementary school, carrying ones and so on.
so we have
123
x 986
_______
Lets do this problem and then make the computer do it the same way
so first we multiply the 6 by 3
we get 18, so we write down 8 in the running total and carry the 1
1
123
x 986
______
8
then we multiply the 6 and the 2
we get 12 so we write down a 2-but carry a 1- so we write down a 3 and carry another 1
1
123
x 986
_______
38
then we multiply the 6 and the 1
we get 6 so we write down a 6 - but carry a 1 - so we write down a 7 and have no carry digit
123
x 986
_______
738
Now we want to move on to the second digit in 986 but we have to think ahead to when we end up summing all the numbers together. so what we're going to do instead of keeping track of a whole number for every digit in the bottom is think of our current product "738" as a carrying number and just add to it every time we need to carry. I'll write it below the current product. Lets call that number the "running total"
so we multiply the 8 by 3, get 24 and write 4 down, and add 2 to the carry value. rinse and repeat
123 123 123
x 986 x 986 x 986
_______ -> _______ _______
040 00840 09840
938 01938 01938
now we have "two products" These end up getting summed at the end of this method anyway, so lets just sum them now.
123
x 986
________
11778
There we go! that's kind of the idea that we'll roll with. so lets imagine we were tackling this problem:
1289763468123498172341123
x 1902384719065519481230986
_____________________________
if you'll notice, 123 and 986 are still at the end of those digits, but we would have continued on the same approach even if we did have this mess. So we'll translate this algorithmic approach to some abstract structures:
1) for every digit in the bottom number,
|1) for each digit of the top number
| multiply the current bottom and current top
| write down the current digit value in the running total
| add any carry values to the next digit
|2) sum the running totals and move on to the next bottom digit (pad with zeros!)
So we kinda know what we're going to do now we can start on our first attempt at a code:
string mulBIGnums(string a,string b){ //function delcaration
if !(isNumerical(a) && isNumerical(b)) return NULLSTRING;
//here is where we would check to see if a and b are made of only numbers and decimal points, etc.
//otherwise we wouldnt be able to multiply them normally
string running_total[] //the number that gets summed every time we move to the next digit
//so, for practical purposes, lets just append to this number from left to right then reverse it later.
string product[]; //our product will also be a string! we will store it here.
int product_current_digit_index = 0;
int product_current_digit //our current product!
int A_current_digit = 0; //our current digit of the top number
int B_current_digit = 0; //current digit of bottom number
//for every digit of the second string, traversing from right to left
//(dont forget the null character at the end so subtract 2 instead of 1 from strlen)
for (int b_current_index = b.strlen()-2; b_current_index >= 0; b_current_index-- ) {
//for every digit in the first string, traversing from right to left
for (int a_current_index = a.strlen()-2; a_current_index >= 0; a_current_index-- ) {
//just like in elementary school, lets just start multiplying numbers
// lets use atio(str s) to convert each char to int because its easy even though it can be unsafe
A_current_digit = atoi( a[i] ); //we've stored the current digit of A into our variable
B_current_digit = atoi( b[i] ); //^ ^ B
//now we multiply those numbers
product_current_digit = A_current_digit * B_current_digit;
//now we have to remember to carry the result to the next digit
//lets strip off anything > 10
product_carry_digit = product_current_digit / 10;
product_current_digit = product_current_digit % 10;
//now we can add the digit to the running total string
/*
insert code to strip a character off of running total, then add and update the rest of the characters
*/
//now we can move on to the next digit!
}//end looping through string A
}//end looping through string B
}//end of function
so this code by no means will compile with no errors. I just threw it together without testing it. but the general structure is there. Division will be similar, but just think "how do i use long division" then break it up into discrete actions and structure an abstract code and write like we did here!
anyway, hope that helps!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Now for division, you build a multiplication table and check to see which multiple of the divisor gives the largest possible partial product. That digit is the first in your quotient. You then subtract that partial product off, and repeat the steps... Ex. 123/45 ->> 45*1 = 45, 45*2=90, 45*3 = 135 <-- too big the first digit in the quotient is 2. 123-90 = 3308/29/20