Question
Complete the method RECURSIVE productOfDigits() that computes the product of the digits of the given positive integer argument, n, except any digits that are 0.
Complete the method RECURSIVE productOfDigits() that computes the product of the digits of the given positive integer argument, n, except any digits that are 0. Note: productOfDigits() should return always return a non-zero answer since it will only be called with non-zero arguments and 0 digits are ignored. e.g., if n = 5403, the method will return: 5*4*3 = 60 (the 0 was ignored) note: the right-most (1's) digit of a positive integer, n, can be found using n%10 (5403 % 10 is 3, for example) the remaining digits (all but the 1's digit) can be found using n/10 (5403 / 10 is 540, for example) So use a recursive call to get the product of all the other digits and multiply it to the last digit if it's not a 0. The base case is when the number is only one digit, so a recursive call would not be needed.
//testing productOfDigits()
System.out.println(productOfDigits(412));
System.out.println(productOfDigits(2000056));
System.out.println(productOfDigits(90));
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started