-
Notifications
You must be signed in to change notification settings - Fork 0
/
Division.cpp
108 lines (104 loc) · 1.91 KB
/
Division.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This code is created by MEHUL PATNI in c++
Last Update date : 17/05/2019
For division of BigInt numbers.
*/
#include<iostream>
#include<string.h>
using namespace std;
string ZeroPadNumber(string ret, int num) // Function adding Padding bits if required.
{
int str_length = ret.length();
for (int i = 0; i < num - str_length; i++)
ret = "0" + ret;
return ret;
}
string Subtraction(string a, string b)
{
static int q = 0;
string sub; // To get the final answer
int m,n;
string sub1;
int subtraction[2] = {0,0};
int l1 = a.length();
int f=1;
if(a.length() == b.length())
{
for(int q = 0; q<a.length(); q++)
{
if(a[q]>b[q])
break;
else if(a[q] == b[q])
continue;
else
{
f=0;
break;
}
}
}
if(a.length() > b.length() || (a.length() == b.length() && f == 1))
{
b = ZeroPadNumber(b,a.length());
for(int i=a.length()-1;i>=-1;i--)
{
if(i == -1 && subtraction[0] != 0)
{
sub1 = subtraction[0]+48;
sub.insert(0,sub1);
break;
}
else if(i == -1)
break;
m = a[i] - 48 - subtraction[0];
n = b[i] - 48;
if(m >=n){
subtraction[1] = (m-n)%10;
subtraction[0] = 0;
}else{
subtraction[1] = (m-n+10)%10;
subtraction[0] = 1;
}
sub1 = subtraction[1]+48;
sub.insert(0,sub1);
}
q++;
return Subtraction(sub,b);
}
else
{
cout<<q<<endl;
return a;
}
}
int main()
{
string a;
cout<<"Enter 1st large number : ";
cin>>a;
string b;
cout<<"Enter 2nd large number : ";
cin>>b;
cout<<endl;
cout<<a<<" // "<<b<<" = ";
string rem = Subtraction(a,b);
int length = rem.length();
while(true)
{
if(rem[0] == 48)
{
for(int x =0;x<rem.length()-1;x++)
{
rem[x] = rem[x+1];
}
}
else
{
break;
}
length--;
}
cout<<a<<" % "<<b<<" = ";
for(int i=0;i<length;i++)
cout<<rem[i];
cout<<endl;
}