Bhuvan loves to play with numbers , his friend Arya gives him two numbers to add. Arya is suffering from Dysgraphia (i.e a deficiency in the ablity to write ) and sometimes writes 6 as 5 and vice versa. given two number X and Y , calculate the minimum and maximum sum, Arya could possiblely get code in javascript
Answers
Answer:
// JavaScript program to find maximum and minimum
// possible sums of two numbers that Arya could possibly get if he is
// replacing digit from 5 to 6 and vice versa.
function replaceDigit(x , from , to)
{
var result = 0;
var multiply = 1;
while (x > 0)
{
var reminder = x % 10;
// Required digit has been found, now replace it
if (reminder == from)
result = result + to * multiply;
else
result = result + reminder * multiply;
multiply *= 10;
x = parseInt(x / 10);
}
return result;
}
// Returns maximum and minimum possible sums of x1 and x2 if digit
// replacements are allowed.
function MinMaxSum(x1 , x2)
{
// Arya will get minimum sum if he replace the digit
// 6 with 5.
var minSum = replaceDigit(x1, 6, 5) + replaceDigit(x2, 6, 5);
// He will always get maximum sum if he will replace
// 5 with 6.
var maxSum = replaceDigit(x1, 5, 6) + replaceDig(x2, 5, 6);
document.write("Minimum sum = " + minSum);
document.write("<br>Maximum sum = " + maxSum);
}
// Driver code
var x1 = 5466, x2 = 4555;
MinMaxSum(x1, x2);