write a code for large nos multilication (upto 200 digits)

Answer Posted / ajeet kumar

#include<iostream>
#include<string>
#include<sstream>
#include<vector>

using namespace std;

string toString(int a){
string s="";
stringstream buf;
buf<<a;
s=buf.str();
return s;
}

string ADD( string n1, string n2 ) {
int l1 = n1.size();
int l2 = n2.size();
int d = l1-l2;
d=(d>0)?d:-d;

string s1="";

if( d > 0 ){
for(int i=0;i<d;i++)
s1=s1+'0';
}
if(l1 > l2)
n2=s1+n2;
if(l2>l1)
n1=s1+n1;
int l = n1.size();
int carry = 0;
int x,y,z;
string ans="";

for(int j=l-1;j>=0;j--){
x=(int)n1[j]-48;
y=(int)n2[j]-48;
z=x+y+carry;

if(z>9){
carry=z/10;
string ad = toString(z%10);
ans=ad+ans;
}
else{
carry=0;
string ad = toString(z);
ans=ad+ans;
}
}
if(carry!=0)
ans='1'+ans;
return ans;
}

string remove_leading_zeroes(string tmp) {
string s="0";
for(int i=0;i<tmp.size();i++){
if(tmp[i]=='0')
continue;
tmp=tmp.substr(i,tmp.size()-i+1);
return tmp;
}
return s;
}

vector <string> equate_with_leading_zeros( string a, string
b ) {
int l1=a.size();
int l2=b.size();
int d=(l1-l2>0)?(l1-l2):(l2-l1);
string s="";
vector<string>v;

for(int i=0;i<d;i++)
s=s+'0';
if(l1>l2)
b=s+b;
if(l2>l1)
a=s+a;

v.push_back(a);
v.push_back(b);
return v;
}

string MULTIPLY( string s1, string s2 ) {
vector<string> v = equate_with_leading_zeros(s1,s2);
string a=v[0];
string b=v[1];
string op1,prev="";
int x,y,z;
int l=a.size();
int counter = 0;
int carry=0;

for(int i=l-1;i>=0;i--){
x = (int)a[i]-48;
op1="";
carry = 0;
for(int j=l-1;j>=0;j--){
y = (int)b[j]-48;
z = x*y+carry;
if(z>9){
op1=toString(z%10)+op1;
carry=z/10;
}
else{
op1=toString(z)+op1;
carry=0;
}
}
if(carry != 0){
op1=toString(carry)+op1;
}
carry=0;
string appString="";
for(int k=0;k<counter;k++)
appString=appString+'0';
op1=op1+appString;
prev = ADD(op1,prev);
counter++;
}
if(carry!=0){
prev = toString(carry)+prev;
}
string ans= remove_leading_zeroes(prev);
return ans;
}

int main()
{
string a,b;
cout<<"Enter first no.: ";
cin>>a;
cout<<"Enter second no.: ";
cin>>b;
string ans = MULTIPLY(a,b);
cout<<ans<<"\n";
return 0;
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between functions abs() and fabs()?

643


What is the use of gets and puts?

599


in any language the sound structure of that language depends on its a) character set, input/output function, its control structures b) character set, library functions, input/output functions its control structures c) character set, library functions, control sturctures d) character set, operators, its control structures

674


Explain why C language is procedural?

766


What is the concatenation operator?

606






Do you know null pointer?

603


What is the difference between pure virtual function and virtual function?

643


How do I read the arrow keys? What about function keys?

610


Why do we need functions in c?

551


I need help with the following lab. Can anyone explain it to me on how to approach this problem. Coding would be great too !!! No busy waiting allowed. Remember that Java monitors are 'signal and continue'. The input data is called the database (DB). In this problem, it consists of a single variable initialized in the main thread, to contain the current time of day in HH:MM:SS:mmm format (mmm is milliseconds) truncated to 3 digits. Threads follow these rules: 1. Readers may all attempt to read at the same time, but NOT if a writer is writing (i.e.; the writer is in the monitor. 2. Writing is exclusive (only 1 writer at a time, no readers while writing). 3. Options: (to be specified by instructor) a. Readers have absolute priority over writers. b. Writers have absolute priority over readers c. When a reader arrives and a writer is waiting, the reader is suspended behind the writer instead of being admitted immediately. Thus, a writer waits for readers that were running when it arrived, but does not to wait for new readers. You may program this in C++ or Java. There are 4 controlling input values: 1. r – number of readers 2. w – number of writers 3. R – delay for a reader to restart 4. W – delay for a writer to restart Create the following program consisting of 1 main process (your main program) with n threads: 1. The main thread: a. Creates the 'n' threads needed. Of these, r of them are readers and w of them are writers. Starts all readers and writers. b. Waits for all 'n' threads to complete. You may use any method to detect when they are all complete. c. Prints out the resulting outputs from the threads. 2. Threads: a. When a thread starts to run, it immediately tries to enter the monitor. b. A reader thread reads the DB, outputs the exact message below, then exits: >>> DB value read =: HH:MM:SS:mmm by reader number: rr where the underlined text is replaced with actual data. c. A writer thread updates the DB with a new value (from the system time), outputs the exact message below, then exits: *** DB value set to: HH:MM:SS:mmm by writer number: ww where the underlined text is replaced with actual data. d. Each thread accesses the data a total of 10 times, exiting, then re-entering the monitor after each access. Hints: 1. File access *might* not be thread-safe, so you should be prepared to handle this. Read the documentation for the language you are using. 2. Suggested values for delays are: a. If looping, R >=1,000,000 and W>=100,000 b. If using “sleep”, then R=100 ms, W=50 ms. Additional: • Thread output is to ONE file used by ALL threads (so you need to synchronize access to it). • Readers must provide sufficient delay that results are useful (delay by R before re-trying). R is another input value. • Writers must also delay. Delay by W, updating the DB each time. W is an input. • Test your program with AT LEAST TWO different sets of values for r and w (#s of readers and writers) plus this set: r = 4, w=2. Basic operation of a thread: attempt to enter the monitor. If unsuccessful, you get put on a queue. When you get in, read or write the data (depending on type of thread), exit the monitor, wait the required delay amount, then try again. Repeat 10 times.

1508


Explain how can you avoid including a header more than once?

595


Explain what is output redirection?

661


Write a C program on Centralized OLTP, Decentralized OLTP using locking mechanism, Semaphore using locking mechanism, Shared memory, message queues, channel of communication, sockets and a simple program on Saving bank application program using OLTP in IPC?

2171


Explain what is the difference between null and nul?

650


Which of these functions is safer to use : fgets(), gets()? Why?

631