Write a public static method name productAll that takes in 2 arguments int a, int b, and returns the product of all values between those two numbers inclusive. Remember a can be less than b, b might be less than a, or they may be equal. They can also be positive or negative.
Example: productAll(2, 5); int returned by method: 120 Example: productAll(5, 2); int returned by method: 120 Example: productAll(3, 3); int returned by method: 9 Example: productAll(3, 5); int returned by method: 60 Example: productAll(-4, 3); int returned by method: 0 Example: productAll(-4, -2); int returned by method: -24 Example: productAll(-5, -2); int returned by method: 120 Example: productAll(-5, 2); int returned by method: 0 Example: productAll(-5, -3); int returned by method: -60. in java
//TestCode.java import java.util.Arrays; public class TestCode { public static int productAll(int a, int b){ int temp; if(a>b){ temp = a; a = b; b = temp; } int result = 1; for(int i = a;i<=b;i++){ result = result * i; } return result; } public static void main(String[] args) { System.out.println(productAll(2,5)); System.out.println(productAll(5,2)); } }
120
120
Get Answers For Free
Most questions answered within 1 hours.