Click here to Skip to main content
15,916,941 members
Articles / Programming Languages / C#
Article

Long Integer Addition/Multiplication

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
3 Jul 20071 min read 31K   145   5   5
This article help you how to add/sub/div/mul long integers

Introduction

In many situations we need to add/multiply etc integers which have length greater than 100 or 500.for example if we want factorial for 5000 it's huge number, can't save in long, double data type. In such situations we need a data structure that will store such type of numbers.

I wrote this program when I had to use long integers in many problems.

Background

you must have programming background and basic mathematics.

Usage

In this article, I will explain the technique that how to add/multiply etc integers which can't fit in the data types. I will use the basic mathematic for it.

For Additions

First reverse the given two integers for example 1009 and 1.now add them

1009

0001

=====

1010

I am not adding extra '0' but just reverse it and the add them

9001

1

====

0101

Now again reverse them you will get 1010 that's the answer.

For Multiply

The main idea behind the multiplication is given as:

For example

1009

*15

====

15135

First reverse the given integers

9001

51

====

5405

x9001

====

53151

Now again add 5405 and x9001 you will get 53151.Reverse it and that's the final answer

15135

Node that, I am using '0' for 'x' in my code.

here is a piece of code for the multipication

public string s1="123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",s2="222222";
private char[] ary,ary1;
private string result="";
private string[] Final;
<code>
private string LongInt(char[] ary,int mult) </code>
<pre>

{


    int num=mult,add=0,mul=0;
    string res="";
    for(int i=0;i<ary.Length;i++)
    {
        int v1=(int)(ary[i]);
        v1=v1-48;
        mul=(v1*num)+add;//multiply number
        string str=mul.ToString();
        //if length greater than 1 then get carry
        if(str.Length>1)
        {
            char[] tmp=str.ToCharArray();
            add=(int)(tmp[0]);
            add=add-48;//carry
            mul=(int)(tmp[1]);
            mul=mul-48;//number
        }
        else
        {
            add=0;//carry
        }
        res +=mul.ToString();//save result
    }

//if carry contain any value then add it with the result

    if(add!=0)
    res +=add.ToString();

    res=Reverse(res);//reverse the integers again
    return res;
}
</pre>
<code>
private string Reverse(string st)</code>
<pre>
{

    char[] ary=st.ToCharArray();
    string ret="";
    for(int i=st.Length-1;i>=0;i--)
    {
        ret +=ary[i];//reverse 
    }

    return ret;
}
</pre>


<code>

public void Mul()</code>
<pre>
{

    //reverse the first integer
    string Org=Reverse(s1);
    ary=Org.ToCharArray();
    //reverse the second integer
    string mult=Reverse(s2);
    ary1=mult.ToCharArray();
    //create final results according to second integer
    Final=new string[ary1.Length];

    for(int i=0;i<ary1.Length;i++)
    {

        int mul=(int)ary1[i];
        mul -=48;//subtract 48 and get original integer 
        result=LongInt(ary,mul);//now multiply the first interger
        //with mul
        Final[i]=Update(result,i);//save the results
    }

    Addition.addition add=new Addition.addition(); 
    string str="";

    for(int i=0;i<Final.Length;i=i+2)
    {
        //here you have to 
        //Console.WriteLine(Final[i]);
        add.s1=Final[i];
        add.s2=Final[i+1];
        str +=add.LongInt(); 
    }

    char[] deli=new char[]{'0'};
    //Trim all '0' from the start
    str=str.TrimStart(deli);
    //display result
    Console.WriteLine(str); 
}
</pre>
<code>

private string Update(string str,int val)</code>
<pre>
{
    
    //add extra '0' to get result
    while(val!=0)
    {
        str =str+"0";
        val--;
    }
    return str;
}
</pre>

now i hope you are able to write your own program for subtraction/division

Comments

if you find and bug/error in the code please reply me,i shall be very thankfull to you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
I did BS in Computer Science from Punjab University,Pakistan.

I have developed lot of Softwares and mostly interested in mathematics,image processing,AI,signal processing,mobile technology,database etc.

Currently working at Confiz Solutions as Sr.Software Engineer.

Comments and Discussions

 
GeneralBigInteger Class Pin
Adam Byrne4-Jul-07 1:07
Adam Byrne4-Jul-07 1:07 
GeneralI'm very confused... Pin
KellyLeahy3-Jul-07 23:45
KellyLeahy3-Jul-07 23:45 
GeneralRe: I'm very confused... Pin
Shakeel Mumtaz4-Jul-07 0:56
Shakeel Mumtaz4-Jul-07 0:56 
GeneralRe: I'm very confused... Pin
KellyLeahy4-Jul-07 7:14
KellyLeahy4-Jul-07 7:14 
GeneralRe: I'm very confused... Pin
KellyLeahy4-Jul-07 7:46
KellyLeahy4-Jul-07 7:46 
Oh... One more thing. You should have a look at the string.Reverse method, it's much faster than the way you are reversing your string.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.