please how do i store large numbers in c. the number has no be large like 121223824824623423476234762347623423476234234342342332762376 maybe up to 100 digits or more. after storing the number i need to carry out mathematical operations on that number which of course has been stored in a variable. mathematical operations like %(modular), and (pow), because as i understand, pow only operates with double while modular works with only integer variables

Recommended Answers

All 3 Replies

Big task.
Luckily, some people have already done some work.
http://www.swox.com/gmp/
If you want to implement on your own, then goodluck. I myself only implemented integers with trivial addition, subtraction and multiplication.

You could embed Python code in C++ and do the calculations with Python. 100+ digit integers are no problem. There is a little code snippet about the embedding of Python code into Dev-C++ at:
http://www.daniweb.com/code/snippet387.html

If you want (or need) to slug it out yourself instead of using someone else's solution, one way is to represent the very large number using an array of char (ie a string). Each char represents a place holder in the actual int. The trick then becomes how to convert char/string to int and back again, and how to deal with carryovers and borrowing. Start out by developing the technique for 2 or 3 digit numbers, then generalize. You can probably get a reasonable idea of how to proceed by searching the board for posts like Huge numbers, or Big numbers.

Classes are useful for encapsulating everything you want to do to/on an object. For example, you can overload the % operator and the pow() function for a class just like you will overload the +-*/ operators for a class. And remember, modulo is just doing division over and over until the remainder is less than the divisor and then returning the remainder rather than whatever the other part of the dividend is called and pow() is just multiplication over and over again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.