-6

I'am trying to write a C code to add, subtract, multiply and divide 2 numbers each containing 100 digits. This should include the use of arrays.

Can anyone please give me any suggestions, a pseudo code or a sample code? To be more clear The user will enter 2 numbers(Integers) Each number can comprise of 100 or less digits i.e integer a can be 10 or 234 or 43582 or 23456788 or 23445667788...... etc. Same for integer b. Now taking these two integers I have to perform the arithmetic operations of Addition,Subtraction,Division,Multiplication,Modulas(%)

caitan correia
  • 41
  • 1
  • 11
  • @brunch875 which standard data type in C can store a 100 digit number? I think none. He wants to implement something like BigInt himself and asks for pointers as to how to get started. – rfreytag Oct 11 '15 at 10:36
  • For multiplication you can use the Karatsuba algorithm: https://en.wikipedia.org/wiki/Karatsuba_algorithm Addition should be fairly easy, just have to carry over the carry bit to the next array element. – rfreytag Oct 11 '15 at 10:39
  • @brunch875 try fitting a 100-digit number in a data type in C – Guillaume Oct 11 '15 at 10:45
  • @Guillaume: According to Henry F, you can fit any number of digits in an `int` type, as long as they are all leading zeroes. – chqrlie Oct 11 '15 at 10:51

2 Answers2

3

You could try using the GMP Library as answered in this question. It can perform Bigint operations in both C and C++. Also if you are fine with C++ code for Bigint you could check out this blogpost.

Community
  • 1
  • 1
Rahul Nori
  • 698
  • 6
  • 17
  • hi Rahul,can you explain this a bit more please.What is the GMP Library used for?? – caitan correia Oct 12 '15 at 14:25
  • @caitancorreia try this [pdf](http://www-oldurls.inf.ethz.ch/personal/fukudak/lect/mssemi/reports/01_rep_DiyoraSalimova.pdf). Please search some more about it, i am sure you will find what you want to know. – Rahul Nori Oct 12 '15 at 16:58
1

You are looking for hints on implementing arbitrary precision arithmetic. Start be reading this article: https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic , then search for bignum c code with a search engine, sample implementations are easy to find. Avoid full blown packages such as gnu MP because they are too advanced and not the right starting point.

chqrlie
  • 131,814
  • 10
  • 121
  • 189