converting this code to subtraction?

How do I convert this code to subtract large numbers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void addition( Node* h1,Node* h2)
{  
	int carry = 0, sum;
Node* result = NULL;
Node* tmp = NULL;
Node* prev = NULL;
while(h1 !=NULL || h2 != NULL)
{ 
	h1->value + h2->value;
	 sum = carry +((h1? h1->value: 0) +(h2? h2->value: 0));
	 carry = (sum >=10)? 1:0;
	 sum = sum % 10;
	 tmp = new Node(sum);
	 if(result== NULL){
		 result = tmp;}
	 else{
		 prev->next = tmp;}
	 prev = tmp;
	 if(h1) h1=h1->next;
	 if(h2) h2=h2->next;
}
if(carry > 0)
	tmp->next = new Node(carry);
Print(result);
}
Last edited on
closed account (o3hC5Di1)
Here's some pointers (no pun intended) to get you on your way:

- Every position in a Node represents one digit of the full number. Knowing this, the addition algorithm sums up 2 equal positions and takes any carry over to the next position.

- Subtracting is a bit different, what do you do different when you subtract two large numbers than when you add them, do it on a paper, then try to code it.

Please let us know where you get stuck or what part specifically you need help with if you need any further help.

All the best,
NwN
Thanks that helped
Topic archived. No new replies allowed.