0% found this document useful (0 votes)
28 views

Java Vlab Assignment6

The document describes a method that accepts a number as a parameter and returns a modified number where each digit is the absolute difference between the original consecutive digits, except for the last digit which is unchanged. It provides the method signature, description, parameter, return type, and logic/algorithm which involves converting the number to a string, extracting each character, calculating the differences, adding them to a new string buffer, and returning the modified number.

Uploaded by

Chatterjee Bubun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Java Vlab Assignment6

The document describes a method that accepts a number as a parameter and returns a modified number where each digit is the absolute difference between the original consecutive digits, except for the last digit which is unchanged. It provides the method signature, description, parameter, return type, and logic/algorithm which involves converting the number to a string, extracting each character, calculating the differences, adding them to a new string buffer, and returning the modified number.

Uploaded by

Chatterjee Bubun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

5.

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

public static int modifyNumber(int number) {

String numberString = String.valueOf(number);

StringBuilder modifiedNumber = new StringBuilder();

// 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

for (int i = 1; i < numberString.length() - 1; i++) {

int diff = Math.abs(Character.getNumericValue(numberString.charAt(i)) -


Character.getNumericValue(numberString.charAt(i + 1)));

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());

You might also like