# include <iostream>
# include <conio.h>
# include <string>
usingnamespace std;
int main()
{
int start = 0;
string sub1[3];
string sub2[3];
int bigint1[3];
int bigint2[3];
string s1 = "245469345927452978149";
string s2 = "927987192697643467899";
for (int i = 0; i < 3; i++)
{
sub1[i] = s1.substr(start, 9);
start = start + 9;
}
start = 0;
for (int i = 0; i < 3; i++)
{
sub2[i] = s2.substr(start, 9);
start = start + 9;
}
int final1[20];
int final2[20];
for (int i = 0; i < 3; i++)
{
bigint1[i] = stoi(sub1[i]);
}
for (int i = 0; i < 3; i++)
{
bigint2[i] = stoi(sub2[i]);
}
int j = 0;
int l = 20;
for (int i = 2; i >= 0; i--)
{
while (bigint1[i] != 0)
{
final2[l] = bigint1[i] % 10;
bigint1[i] = bigint1[i] / 10;
l--;
j++;
}
}
int z = 20;
for (int i = 2; i >= 0; i--)
{
while (bigint2[i] != 0)
{
final1[z] = bigint2[i] % 10;
bigint2[i] = bigint2[i] / 10;
z--;
}
}
cout << "\n\n";
for (int i = 0; i <= 20; i++)
{
cout << final1[i];
}
cout << "\n\n";
for (int i = 0; i <= 20; i++)
{
cout << final2[i];
}
int final3[20];
for (int i = 20; i >= 0; i--) //this for loop is used for subtraction by using borrow but its not giving rigth value from 15th to 0th index
{
if (final1[i] >= final2[i])
{
final3[i] = final1[i] - final2[i];
}
else
{
for (int j = i - 1; j >= 0; j--)
{
if (final1[j] > final2[j])
{
final1[j] = final1[j] - 1;
for (int z = j - 1; z <= i; z++)
{
if (z == i)
{
final1[i] = final1[i] + 10;
final3[i] = final1[i] - final2[i];
}
else
{
final1[z] = final1[z] + 9;
}
}
break;
}
}
}
}
cout << "\n\n";
for (int i = 0; i <= 20; i++)
{
cout << final3[i];
} //it is giving write value from 20th to 15th
_getch();
return 0;
}
#include <iostream>
#include <string>
#include <cctype>
// true if every character in num_str is a decimal digit
bool is_number( const std::string& num_str )
{
for( char c : num_str ) if( !std::isdigit(c) ) returnfalse ;
returntrue ;
}
// prefix the shorter string with '0's so that a and b are of the same size
void make_same_size( std::string& a, std::string& b )
{
while( a.size() < b.size() ) a = '0' + a ;
while( b.size() < a.size() ) b = '0' + b ;
}
std::string minus( std::string a, std::string b )
{
if( is_number(a) && is_number(b) ) // if both are numbers
{
make_same_size( a, b ) ; // make them to be of the same size
if( a == b ) return"0" ;
// note that it is guaranteed that '3' > '2' etc.
if( b > a ) return"error: negative" ; // can't subtract, return error string
// convert both strings to hold integer values of the digits
for( char& c : a ) c -= '0' ; // '7' - '0' == 7 etc
for( char& c : b ) c -= '0' ;
// perform the subtraction a -= b
int borrow = 0 ;
for( int i = a.size() - 1 ; i >= 0 ; --i )
{
constint r = a[i] - b[i] - borrow ;
if( r < 0 ) { /* negative r, there is a borrow */ a[i] = r+10 ; borrow = 1 ; }
else { /* non-negative r, no borrow */ a[i] = r ; borrow = 0 ; }
}
// convert a from integer values back to characters
for( char& c : a ) c += '0' ; // 5 + '0' == '5' etc.
// return a after removing leading zeroes
int pos = 0 ;
while( !a.empty() && a[pos] == '0' ) ++pos ;
return a.substr(pos) ;
}
return"not a number" ; // not number, return error string
}
int main()
{
std::string s1 = "245469345927452978149";
std::string s2 = "555237987192697643467899";
std::cout << s2 << " -\n" << std::string( s2.size() - s1.size(), ' ' ) << s1
<< "\n----------------------------\n" << minus(s2,s1) << '\n' ;
}