If u wanna code her, You must feel her!

Stirng : Sum Of Two Large Numbers

String : Sum of Two Large Numbers

Can you find out the sum of two 100 digit numbers using C Programming?

If you cannot do it, then this article is for you. Let's get started!

  • If we want to add two 100 digit numbers, it is neither possible using long long datatype in C Programming Language nor C++ Language. As long long datatype gives you access up to 18 digit numbers, you have to focus on using string now. Because string can store 10^7 digit numbers as we discussed in another article. Here we will see how we can merge our ideas with the features of string to add two numbers. At first, we will build up an algorithm here, then we will implement it.

What do we do when we add two numbers writing in papers? Do You remember that process?


  • Suppose, we want to add 83 and 79. What do we do?
    
        We write them like this:-
                83
              +79
        ---------
        Then we calculate 3+9 = 12.
        We place 2 at the units place and consider 1 as a carry.
                83
              +79
        ---------
                 2
        Then we calculate 8+7+1 = 16.
        We place 6 at the tens place and consider 1 as a carry.
                83
              +79
        ---------
                62
        As there are no digits left, we place 1 at the hundreds place.
                83
              +79
        ---------
              162


This is the algorithm that we are going to use. But before that let's work with another example.

  • We want to add 9876 and 55. what do we do?

        We write them like this:-
                9876
                 +55
        ------------
        Calculate 6+5 = 11.
        Place (11 % 10) = 1 at units place & carry = (11 / 10) = 1.
                9876
                 +55
        ------------
                     1
        Calculate 7+5+1 = 13.
        Place (13 % 10) = 3 at tens place, carry = (13 / 10) = 1.
                9876
                 +55
        ------------
                   31
        Calculate 8+0+1 = 9.
        Place (9 % 10) = 9 at hundreds place, carry = (9 / 10) = 0.
                9876
                 +55
        ------------
                  931
        Calculate 9+0+0 = 9.
        Place (9 % 10) = 9 at thousands place, carry = (9 / 10) = 0.
                9876
                 +55
        ------------
                9931
        As there are no digits left, we place 0 at ten thousands place.
                9876
                 +55
        ------------
              09931

  • So, if the two numbers are not of equal lengths, we have to add leading zeros to the smaller number. This part is very important.
  • At first, we will write a code that will work for two numbers if their lengths are equal (if not equal then we will set leading zeros). But if we do not want to set leading zeroes by our hand and we will just give input as the numbers are then we will have to turn the length of the smaller string into equal as the larger string by adding leading zeroes. You must try this part by yourself. Here we will write a code for two string of equal length.
  • Now, Let char num1[] = "12", num2[] = "34";. We do the following operation :- int s = num1[1] + num2[1];. Now what is the value of s? Is it 6? Let's type it in our editor and compile to see the result. [You have to do it by your own to make your idea more clear.] The value is not 6, is it? Actually here, in num1 & num2 , 1,2,3,4,etc are characters. They are not numbers. And when we add two characters, we actually add their ASCII values. So, we do not get the ans 6. However, to solve this awkward situation, we can subtract the ASCII value of '0' from the characters. But we can subtract character '0' instead of its ASCII value and if we make the following operation - int s = (num1[1] - '0') + (num2[1] - '0'), the value of s will be as we expected. We will use this convention in our code. So this is one of the most important idea. 
  • We have to keep in mind that we start from the end of the string to the beginning. The length of the summation storing string must be 1 greater than the length of the other two strings. Let's get started!

  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char num1[] = "98765";
  6.         char num2[] = "87654"; // Instead you can scan them
  7.         int len = strlen(num1); // Instead you can write len = strlen(num2)
  8.         char sum[len+2];    // We did not take len + 1, but len + 2. Reason will bw explained later.
  9.         int i, j, carry, s;
  10.  
  11.         //in the first addition, carry = 0. So
  12.         carry = 0;
  13.         for(= len - 1, j = len; i >= 0; i--, j--) {
  14.             s = (num1[i] - '0') + (num2[i] - '0') + carry;  // Converting to numbers
  15.             carry = s / 10;
  16.             sum[j] = (% 10) + '0';    // Converting into a character
  17.             //why did we use j as indexe for sum[] and why not i? I'll leave it to you.
  18.         }
  19.         //After this loop ends, one carry will be pending. We have to store it into the sum[] string
  20.         sum[j] = carry + '0';   // Converting into a character
  21.         sum[len + 1] = '\0';    /* If we do not do this, then we will get garbage values 
  22.                                 at the end of our output. And to set this NULL character,
  23.                                 we took one more place for this string*/
  24.         printf("%s + %s = %s\n", num1, num2, sum);
  25.         return 0;
  26.     }


So, we can now calculate the sum of two equal length large numbers! But here we will get 9876+55 = 09931. How are we gonna get rid of this zero? this is your task to do.


Thank you for reading the whole article. Hope it might be a help for your programming career!


Happy Coding!

No comments

Theme images by enot-poloskun. Powered by Blogger.