Click here to Skip to main content
Licence 
First Posted 3 Jul 2007
Views 15,185
Downloads 74
Bookmarked 5 times

Long Integer Addition/Multiplication

By | 3 Jul 2007 | Article
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>

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

About the Author

Shakeel Mumtaz

Web Developer

Pakistan Pakistan

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBigInteger Class PinmemberAdam Byrne1:07 4 Jul '07  
GeneralI'm very confused... PinmemberKellyLeahy23:45 3 Jul '07  
GeneralRe: I'm very confused... PinmemberShakeel Mumtaz0:56 4 Jul '07  
GeneralRe: I'm very confused... PinmemberKellyLeahy7:14 4 Jul '07  
GeneralRe: I'm very confused... PinmemberKellyLeahy7:46 4 Jul '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 4 Jul 2007
Article Copyright 2007 by Shakeel Mumtaz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid