Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

define a method printval() that has two integer parameters and outputs …

Question

define a method printval() that has two integer parameters and outputs the product of all positive integers, starting with the first integer parameter and ending with the second, followed by a newline. if no positive integers exist, assign product with 1. printval() should not return any value.
ex: if the input is -4 5, then the output is:
120
note: positive numbers are greater than 0.
2
3 public class valueprinter {
4
5 / your code goes here /
6
7 public static void main(string args) {
8 scanner scnr = new scanner(system.in);
9 int in1;
10 int in2;
11
12 in1 = scnr.nextint();
13 in2 = scnr.nextint();
14
15 printval(in1, in2);
16 }

Explanation:

Step1: Initialize product

Initialize a variable product to 1. This will store the product of the positive integers.

int product = 1;

Step2: Determine the range

Find the minimum and maximum of the two input integers. Then loop from the minimum to the maximum.

int min = Math.min(num1, num2);
int max = Math.max(num1, num2);
for (int i = min; i <= max; i++) {

Step3: Check if positive

Check if the current integer i is positive. If it is, multiply it to product.

if (i > 0) {
    product *= i;
}

Step4: Output the result

After the loop, print the product.

System.out.println(product);

Complete Code

public class ValuePrinter {
    public static void printVal(int num1, int num2) {
        int product = 1;
        int min = Math.min(num1, num2);
        int max = Math.max(num1, num2);
        for (int i = min; i <= max; i++) {
            if (i > 0) {
                product *= i;
            }
        }
        System.out.println(product);
    }

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int in1;
        int in2;

        in1 = scnr.nextInt();
        in2 = scnr.nextInt();

        printVal(in1, in2);
    }
}

Answer:

Step1: Initialize product

Initialize a variable product to 1. This will store the product of the positive integers.

int product = 1;

Step2: Determine the range

Find the minimum and maximum of the two input integers. Then loop from the minimum to the maximum.

int min = Math.min(num1, num2);
int max = Math.max(num1, num2);
for (int i = min; i <= max; i++) {

Step3: Check if positive

Check if the current integer i is positive. If it is, multiply it to product.

if (i > 0) {
    product *= i;
}

Step4: Output the result

After the loop, print the product.

System.out.println(product);

Complete Code

public class ValuePrinter {
    public static void printVal(int num1, int num2) {
        int product = 1;
        int min = Math.min(num1, num2);
        int max = Math.max(num1, num2);
        for (int i = min; i <= max; i++) {
            if (i > 0) {
                product *= i;
            }
        }
        System.out.println(product);
    }

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int in1;
        int in2;

        in1 = scnr.nextInt();
        in2 = scnr.nextInt();

        printVal(in1, in2);
    }
}