subtracting two large numbers
C++
// C++ program to find difference of two large numbers.
#include<bits/stdc++.h>
using namespace std;
// Returns true if str1 is smaller than str2.
bool isSmaller(string str1, string str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i=0; i<n1; i++)
if (str1[i] < str2[i])
return true;
else if (str1[i] > str2[i])
return false;
return false;
}
// Function for find difference of larger numbers
string findDiff(string str1, string str2)
{
// Before proceeding further, make sure str1
// is not smaller
if (isSmaller(str1, str2))
swap(str1, str2);
// Take an empty string for storing result
string str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int carry = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i=0; i<n2; i++)
{
// Do school mathematics, compute difference of
// current digits
int sub = ((str1[i]-'0')-(str2[i]-'0')-carry);
// If subtraction is less then zero
// we add then we add 10 into sub and
// take carry as 1 for calculating next step
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0');
}
// subtract remaining digits of larger number
for (int i=n2; i<n1; i++)
{
int sub = ((str1[i]-'0') - carry);
// if the sub value is -ve, then make it positive
if (sub < 0)
{
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0');
}
// reverse resultant string
reverse(str.begin(), str.end());
return str;
}
// Driver code
int main()
{
string str1 = "978";
string str2 = "12977";
cout << findDiff(str1, str2) << endl;
string s1 = "100";
string s2 = "1000000";
cout << findDiff(s1,s2);
return 0;
}
Also in C++:
- Title
- create copy constructor c++
- Category
- C++
- Title
- appending a double to a string c++
- Category
- C++
- Title
- mkdir c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- getline not working c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- how to initialize an struct object in c++
- Category
- C++
- Title
- c++ clamp
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- how to run a c++ program in the background
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- gfg left view of tree
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- how long can a c++ string be
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- what is sigsegv error in c++
- Category
- C++
- Title
- insertion c++
- Category
- C++
- Title
- cout value c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- converting char to int in c++
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- c++ initialize array
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- How to read a file in in C++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- c++ ambigous error
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- container class in c++
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- for c++
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- transpose matrix eigen c++
- Category
- C++
- Title
- traverse map c++
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- font awesome bootstrap cdn
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- glfw initialize in c++
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- typeid to string c++
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- how to print a 2d array in c++
- Category
- C++
- Title
- how to append an element to an array in cpp
- Category
- C++
- Title
- loop through words in string c++
- Category
- C++
- Title
- if not defined c++
- Category
- C++
- Title
- how are graphics in games made
- Category
- C++
- Title
- c++ iterate over vector
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++