50 character long values addition

Posted on 2004-12-01
Last Modified: 2010-04-01
I would like to add 2 values each having 50 chars value in them. Like,

int a = 1542232154845421451787....upto 50 char
int b = 4545214787956231545451.....upto 50 char

Now if I add them and result will come more than that of INT/Double/Long bytes, I just want these two numbers to be added and store and then display the complete result on the screen without any overflow or any bound number of digits. How can I use these, is there any special data type for it.
Question by:vicky2k
    LVL 3

    Expert Comment

    Have a look at:
    LVL 20

    Expert Comment

    why not using 64-bit-datatypes

    if you are on windows use "__int64" or other systems: "signed long long" rather than int's

    hope it helps :)
    LVL 86

    Expert Comment

    You might want to check out the Numeric Template Library (NTL) at - it provides support for arbitrary lenght integers.
    LVL 55

    Accepted Solution

    If just want to add/subtract/compare, then I suggest this simple class, BigInt:
    Good luck,

    Author Comment

    Make a program in C++ and then tell me here the codes becuase I dont have enough time to understand the classes made for this, I need simple example like 2n+5n=615454545454545445 as long as number evaluated from calculations it should be printed on screen in full.
    LVL 9

    Assisted Solution

    sounds like a hw q. here's an outline/skeleton

    char num1[50] = "000....00012345678901234567890", num2[50] = "000...00098765432109876543210";

    add(char * result, const char * num1, const char * num2){
      memcpy(result, num1, 50*sizeof(char));
      for(i=49; i>=0; i--){
        result[i] += num2[i];
        if((result[i] - '0'*2) > 10){
          //do carry over operation
      cout << result; //need to take care of that 0-padding
    I have given you the frame, now fill out the missing part and that should be it. In case you have question about '0'*2:
    if you add '9' and '5' you get ('0'+9) + ('0'+5). subtracting 2*'0' will give you 14 and then you know that a carry over action is needed.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now