This is a simple challenge I’ve seen recently. I’ve looked around the web and found that I didn’t care for the Java implementations, so put my mind to work and came up with a new solution.
int countCarryOperations(int a, int b) {
int multiplier = 10;
int count = 0;
int modA = 0;
int modB = 0;
int carryOver =0;
do {
int Ax = ((a % multiplier ) - modA) / (multiplier /10);
int Bx = ((b % multiplier ) - modB) / (multiplier /10);
int x = Ax+Bx + carryOver;
if(x >= 10) {
count++;
carryOver = 1;
} else {
carryOver = 0;
}
modA = a % multiplier;
modB = b % multiplier;
if(modA == a && modB == b) {
break;
}
multiplier *= 10;
}while(true);
return count;
}
Discover more from GhostProgrammer - Jeff Miller
Subscribe to get the latest posts sent to your email.