Java Vlab Assignment6
Java Vlab Assignment6
Using Java Create a method that accepts a number and modifies it such that the
each of the digit in the newly formed number is equal to the difference between
two consecutive digits in the original number. The digit in the units place can be
left as it is. Note: Take the absolute value of the difference. Ex: 6-8 = 2
Method Name modifyNumber
Method Description Accepts a number and modify it as per the
requirement
Argument int number1
Return Type int
Logic Accept a number and modify it such that the
each of the digit in the newly formed number is
equal to the difference between two consecutive
digits in the original number. For
example. Input: 45862
Output:13242 Algorithm:
1. Convert number into String
2.Extract each char using charAt method
3. Convert char to int and find the difference
4.Create new StringBuffer object and keep
adding the difference
5. Finally convert StringBuffer to int
// Add the difference between the first two digits to the modified number
modifiedNumber.append(Math.abs(Character.getNumericValue(numberString.charAt(0)) -
Character.getNumericValue(numberString.charAt(1))));
// Calculate and add the difference between consecutive digits to the modified number
modifiedNumber.append(diff);
}
// Add the last digit as it is to the modified number
modifiedNumber.append(numberString.charAt(numberString.length() - 1));
return Integer.parseInt(modifiedNumber.toString());