How can a subtraction be done?
Deciding on the stopping condition - you want your while loop to continue until both numbers a, b, and carry turn zero. In other words, the condition should be a || b || carry
Adding the next digit to the sum - since the result is coded as decimal, you need to multiply digit d by the next consecutive power of ten. A simple way of doing that would be adding a new variable m which starts at 1 and gets multiplied by ten each iteration.
Can somebody please help make subtraction:
static int addition(int num1, int num2)
{
int sum = 0, digit = 0, carry = 0, digit_rank = 1;
// Calculate the sum
while (num1 > 0 || num2 > 0 || carry > 0)
{
// Calculate the digit
digit = num1 % 10 + num2 % 10 + carry;
// Determine if you should carry or not
if (digit > 7)
{
carry = 1;
digit %= 8;
}
else
carry = 0;
// Add the digit at the beggining of the sum
sum += digit * digit_rank;
digit_rank *= 10;
// Get rid of the digits of a and b we used
num1 /= 10;
num2 /= 10;
}
return sum;
}