Jump to content

Addition with extremely huge numbers in C++

- - - - -

  • Please log in to reply
No replies to this topic

#1
Csabi

Csabi

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
In C++ you the data type with the largest range is the long long, with a maximum limit of ~19 digits. This means that you can`t make additions with numbers that result a larger number. I know this is a crazy thing, why would you need to calculate with such huge numbers ? I think never..., but the method is very useful and ingenious.
Here is my code:
#include <iostream>

#include <string>


using namespace std;


int main(){

   string nr;

   bool validnr = false;

   while(!validnr){

      cout << "Enter the first number: " << endl;

      cin >> nr;

      validnr = true;

      [COLOR="lime"]//Check the number for invalid characters[/COLOR]

      for(int i=0;i<nr.length();i++){

         if((int) nr[i] < 48 || (int) nr[i] > 57){

         [COLOR="lime"]//48 is the ASCII code of number 0

         //57 is the ASCII code of number 9[/COLOR]

            validnr = false;

         }

      }

      if(!validnr){

         cout << endl << "Please enter a valid number!" << endl;

      }

   }

   string nr2;

   validnr = false;

   [COLOR="lime"]//Do the same for the second number[/COLOR]

   while(!validnr){

      cout << "Enter the second number: " << endl;

      cin >> nr2;

      validnr = true;

      for(int i=0;i<nr2.length();i++){

         if((int) nr2[i] < 48 || (int) nr2[i] > 57){

            validnr = false;

         }

      }

      if(!validnr){

         cout << endl << "Please enter a valid number!" << endl;

       }

   }

   int digitsnr;

  [COLOR="lime"] //Make the two digits to have the same size

   //by adding 0`s in front of it

   //This is important because we can`t add a digit with nothing[/COLOR]

   if(nr.length() > nr2.length()){

      digitsnr = nr.length() - 1;

      while(nr.length() != nr2.length()){

         nr2 = "0" + nr2;

       }

   }

   else{

      digitsnr = nr2.length() - 1;

      while(nr.length() != nr2.length()){

         nr = "0" + nr;

      }

   }

   string result;

   char digit;

   int remainder = 0; //If the result of two digits is more than 10

   [COLOR="lime"]//we need to add 1 to the following digit(e.g: 5 + 7 = 12, 12 is made from 2 digits[/COLOR]

   for(int i=digitsnr;i>=0;i--){ //Loop through each digit

      if(remainder + nr[i] + nr2[i] - 96 > 9){

         digit = remainder + nr[i] + nr2[i] - 48 - 10; //We are working with ASCII codes

         result = digit + result;

         remainder = 1; [COLOR="lime"]//We have to increase the following digit[/COLOR]

      }

      else{

         digit = remainder + nr[i] + nr2[i] - 48;

         remainder = 0;

         result = digit + result;

      }

   }

   if(remainder){

      result = "1" + result;

     [COLOR="lime"] //Add the last remaining if required[/COLOR]

   }

   cout << endl << "Result = " << result << endl;

   system("pause");

}
The numbers are stored in strings, each character of the strings is checked to avoid wrong characters. Each character is checked using it`s ASCII code, (for example 0 has the 47 code number, 1 has the 49 code). After the numbers are validated the two numbers are added digit by digit starting from the last number:
123 + 56 =

9

79

179
Is that simple, but if something it`s not clear please don`t hesitate, ASK ME! (Here is the original article with more details: addition with huge numbers)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users