A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a Java program to plus one to the number of a given positive numbers represented as an array of digits
Q:

Write a Java program to plus one to the number of a given positive numbers represented as an array of digits

0

Write a Java program to plus one to the number of a given positive numbers represented as an array of digits

Sample array: [9, 9, 9, 9] which represents 9999
Output: [1, 0, 0, 0, 0].

Expected Output:

Original array: [9, 9, 9, 9]
Array of digits: [1, 0, 0, 0, 0]

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
public class Solution {
 public static void main(String[] args) {
  int[] nums = {9,9,9,9};
  System.out.println("Original array: " + Arrays.toString(nums));
  System.out.println("Array of digits: " + Arrays.toString(plus_One_digit(nums)));
 }
 public static int[] plus_One_digit(int[] digits_nums) {
  for (int i = digits_nums.length - 1; i > -1; --i) {
   if (digits_nums[i] != 9) {
    digits_nums[i] += 1;
    for (int j = i + 1; j < digits_nums.length; ++j) {
     digits_nums[j] = 0;
    }
    return digits_nums;
   }
  }
  int[] result = new int[digits_nums.length + 1];
  result[0] = 1;
  return result;
 }
}

Sample Output:

Original array: [9, 9, 9, 9]
Array of digits: [1, 0, 0, 0, 0]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now