Name: Udaykumar Upputuri
Description: Developed an Arbitrary Precision Calculator in C language
Mail: udayupputuri3525@gmail.com
The Arbitrary Precision Calculator (APC) is a command-line calculator written in C that performs arithmetic operations on very large integers that exceed the limits of normal C data types.
Unlike traditional calculators that use fixed-size variables (int, long, long long), this calculator can handle numbers with hundreds or thousands of digits.
This project uses a Doubly Linked List data structure where each node stores a single digit of the number. Arithmetic operations are performed digit-by-digit similar to manual calculations.
Arbitrary Precision Arithmetic means performing mathematical operations on numbers with no fixed size limit. The only limitation becomes system memory.
Example:
Normal C integer limit : 9223372036854775807
APC example: 999999999999999999999999999999999999999999999999
Normal calculators or C programs use fixed data types, which cause:
Integer overflow
Loss of precision
Limited digit storage
Example in C: long long num = 999999999999999999999;
This causes overflow.
APC avoids this by storing each digit in a linked list node, allowing unlimited digits.
Addition, Subtraction, Multiplication, Division of very large numbers
Supports positive and negative integers
Handles numbers with unlimited digits
Command line argument based input
Example execution:
./a.out 123456789123456789 + 987654321987654321
Udaykumar_APC
├── main.c
├── apc.c
├── apc.h
├── add.c
├── sub.c
├── mul.c
├── div.c
└── README.md
File Description
main.c Entry point of the program. Handles input, validation and operation selection
apc.h Header file containing structure definitions, macros and function declarations
apc.c Utility functions and operation decision logic
add.c Contains addition logic
sub.c Contains subtraction logic
mul.c Contains multiplication logic
div.c Contains division logic
README.md Project documentation
1️⃣ User Input
User provides input through command line arguments:
./a.out operand1 operator operand2
Example: ./a.out +12345 + -6789
2️⃣ Input Validation
The program validates:
Number of arguments
Valid operator
both operands must contain numerical characters, if any alpha or special print error
Correct sign usage
Function used: validate_operand()
3️⃣ Sign Detection
The program checks whether operands are positive or negative using flags:
positive_flag1, negative_flag1
positive_flag2 negative_flag2
4️⃣ Convert Operands to Linked Lists
fetching character by character for each operand, converting character into digit
and storing that digit into linked list, each time one one digit get extracted and stored
Example number: 12345
Stored as list: 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5
Function used: insert_last()
5️⃣ Remove Leading Zeros
Example: 000123
Converted to: 123
Function used: remove_pre_zeros()
6️⃣ Operation Decision
Based on the operator and signs of operands, the program decides whether to perform:
Addition or Subtraction
Function used: operation_decider()
7️⃣ Perform Arithmetic Operation
Operation Function
Addition addition()
Subtraction subtraction()
Multiplication multiplication()
Division division()
8️⃣ Print Result
The result stored in the linked list is printed digit by digit.
Function used: print_list()
Addition is performed similar to manual addition.
Example:
456
+ 789
------
1245
Steps:
Traverse digits from right to left - if any operand becomes NULL, its digit is taken as 0
extracts digits and perfrom addition - Add digits with carry
calculate result digit and update carry
insert result digit - inserted at the front of the result list
and after move the both Pointers to the previous nodes.
Formula:
sum = digit1 + digit2 + carry
data = sum % 10
carry = sum / 10
Digits are inserted at the front of the result list.
Subtraction also follows manual subtraction.
Example:
500
- 275
-----
225
Steps:
1st finding the greater number, check length if same compare each character both operand to find which operand is larger,
if op1> op2 -> pass to subtraction function like head1, head2
if op2 > op1 -> pass to subtraction function like head2, head1, reverse
Traverse digits from right to left
Adjust Digit if Borrow Exists, Check Borrow Condition
Perform Subtraction, store result digit (Insert result digit at the front of result list)
move to previous nodes
after all remove the leading zeros to avoid printing zeros in output
Borrow logic:
digit1 += 10
borrow_flag = 1
Multiplication follows long multiplication method.
Example:
123
× 45
------
615
+ 4920
------
5535
Steps:
Multiply each digit of operand2 with operand1
Shift digits with zeros
Add intermediate results
Temporary result lists:
headR1 -> accumulated result
headR2 -> partial result
Division uses repeated subtraction.
Example:
258 ÷ 12
Steps:
Extract digits from dividend
Compare with divisor
Subtract divisor repeatedly
Count subtraction operations
That count becomes the quotient digit.
Functions used:
compare_list()
subtraction()
insert_last()
Structure:
typedef struct node
{
struct node *prev;
int data;
struct node *next;
} node;
Each node stores one digit of the number.
Example: 12345
Linked list representation: Head → 1 ⇄ 2 ⇄ 3 ⇄ 4 ⇄ 5 ← Tail
Function Purpose
insert_first() Insert digit at beginning
insert_last() Insert digit at end
delete_first() Remove first node
delete_list() Delete entire list
compare_list() Compare two numbers
list_len() Calculate length of number
remove_pre_zeros() Remove leading zero
Arbitrary precision arithmetic is used in:
Cryptography
Blockchain systems
Scientific computing
Financial calculations
High precision mathematics
Computer algebra systems
Large factorial calculations
Example: 1000! (factorial of 1000)
This number contains 2568 digits.
Handles extremely large numbers
No integer overflow
Accurate arithmetic operations
Demonstrates advanced data structures
Good practice for linked list algorithms
Slower than normal arithmetic operations
Uses more memory
Implementation complexity is higher
Not ideal for small calculations