-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKARATSUBAMULTIPLICATION.CPP
77 lines (77 loc) · 1.36 KB
/
KARATSUBAMULTIPLICATION.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Karatsuba-Multiplication
pheew after so much of effort.Please find cases where the program fall short
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
char ch1[100],ch2[100],w[50],x[50],y[50],z[50],ch3[50];
main()
{
int j,m,n,i,e,f;
unsigned long long a=0,b=0,c=0,d=0;
cout<<"Enter the numbers to be multiplied:";
cin>>ch1;
cin>>ch2;
cout<<"ch1="<<ch1<<"\n"<<"ch2="<<ch2<<"\n";
m=strlen(ch1);
n=strlen(ch2);
cout<<m<<" "<<n<<"\n";
if(m>n)
{
e=m;
for(i=0;i<(m-n);i++)
ch3[i]='0';
ch3[i]='\0';
cout<<"ch3= "<<ch3<<"\n";
strcat(ch3,ch2);
cout<<"ch3= "<<ch3<<"\n";
strcpy(ch2,ch3);
cout<<"ch2="<<ch2<<"\n";
}
else if(m<n)
{
e=n;
for(i=0;i<(n-m);i++)
ch3[i]='0';
ch3[i]='\0';
cout<<"ch3= "<<ch3<<"\n";
strcat(ch3,ch1);
cout<<"ch3= "<<ch3<<"\n";
strcpy(ch1,ch3);
cout<<"ch1="<<ch1<<"\n";
}
else
{
e=n;
cout<<"ch2="<<ch2<<"\n";
}
f=e/2;
cout<<"f="<<f<<"\n";
for(i=0;i<f;i++)
w[i]=ch1[i];
w[i]='\0';
cout<<"w="<<w<<"\n";
for(;i<e;i++)
x[i-f]=ch1[i];
x[i]='\0';
cout<<"x="<<x<<"\n";
for(i=0;i<f;i++)
y[i]=ch2[i];
y[i]='\0';
cout<<"y="<<y<<"\n";
for(;i<e;i++)
z[i-f]=ch2[i];
z[i]='\0';
cout<<"z="<<z<<"\n";
a=atoll(w);
cout<<"a="<<a<<"\n";
b=atoll(x);
cout<<"b="<<b<<"\n";
c=atoll(y);
cout<<"c="<<c<<"\n";
d=atoll(z);
cout<<"d="<<d<<"\n";
cout<<"Result:"<<a*c*pow(10,e)+(a*d+b*c)*pow(10,f)+b*d<<"\n";
}