You are to write a program that will multiply two numbers and print out the two
ID: 3542089 • Letter: Y
Question
You are to write a program that will multiply two numbers and print out the two numbers and the results. Your program should handle up to 30 digit integer numbers.
Your input for this program is found in BigNumberV2.dat
The input format is one number per line. You will have several pairs of numbers to process.
Label your output.
Format of output: Print out the two numbers you read in and the product. I want the three numbers are right one number per line, right hand justified and NO leading zeros.
Example:
..............12222
.......x 5555555
---------------------
679011043210
___________________________________________________________________________________________________
Input Data (BigNumberV2.dat)
Explanation / Answer
please rate - thanks
check your example--the calculator agrees with my answer
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;
bool inputNum(int [],int&,istream&);
void multiply(int[],int,int[],int,int[],int&);
void print(int[],int,int,int);
int main()
{ifstream input;
int num1[35],num2[35],len1,len2,num3[60],len3=10,i;
input.open("BigNumberV2.dat"); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
while(inputNum(num1,len1,input))
{inputNum(num2,len2,input);
multiply(num1,len1,num2,len2,num3,len3);
print(num1,len1,len3,1);
print(num2,len2,len3,2);
for(i=0;i<len3;i++)
cout<<"-";
cout<<endl;
print(num3,len3,len3,1);
//cout<<len1<<" "<<len2<<" "<<len3<<endl;
cout<<endl;
}
system("pause");
}
void print(int num[],int len, int maxlen,int n)
{int i,count;
count=maxlen-len;
if(n==2)
count--;
for(i=0;i<count;i++)
cout<<" ";
if(n==2)
cout<<"x";
for(i=len-1;i>=0;i--)
cout<<num[i];
cout<<endl;
}
void multiply(int num1[],int len1,int num2[],int len2,int num3[],int& len3)
{int i,j,tmp ;
for(i=0;i<60; i++)
num3[i]=0;
for(i=0;i<len2;i++)
{ len3=i;
for(j=0;j<len1;j++)
{tmp=num1[j]*num2[i];
num3[len3]=num3[len3]+tmp;
num3[len3+1]=num3[len3+1]+num3[len3]/10;
num3[len3]%=10;
len3++;
}
}
if(num3[len3]!=0)
len3++;
}
bool inputNum(int integer[],int& len,istream& file)
{string in;
int j,k;
for (len=0;len<35;len++ )
integer[len]=0;
len=35-1;
file>>in;
if(!file)
return false;
for(len=0;in[len]!='';len++);
k=0;
for(j=len-1;j>=0;j--)
integer[j]=in[k++]-48;
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.