Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JGRASP Only (Java) B03 - Multiply even digits Write a program that will ask the

ID: 3913058 • Letter: J

Question

JGRASP Only (Java) B03 - Multiply even digits Write a program that will ask the user for a number, and then recursively computes the multiplication of every even number that is less than the given number. That is, if the user enters 13, your program would recursively compute 12*10*8*6*4*2. Hint: Define the following method: public static long computeMultiplication(int n) Note: Validate the user input to make sure the entered number is a positive integer. Otherwise, keep asking the user for a number until the input is valid.

Explanation / Answer

Following program is to Multiply even digits -

create file name Even_digit_multiplication.java and write following code in it and you will get desired results -

I also, comment out some lines where explanation needed if you want more explanation then ask me I am here to help you and do comment your feedback below about this solution -

................................................................................................................................

import java.util.Scanner; // this is required to use scanner in our file to take the user input

class Even_digit_multiplication {
int n;
static long computeMultiplication(int n){
long Product = 1;
for (int x=1;x<=n;x+=1)
{
if (x%2 == 0) {
Product *= x;
}
}
return Product;
}

public static void main(String[] args) {

long product = 1;
System.out.println("enter the number");
Scanner in = new Scanner(System.in);
int number = in.nextInt(); //this will be the limit till we have to scan for even numbers

if(number<0){

do { //whole do while loop is for negative inputs which runs till we get valid positive number

System.out.print("Please enter a positive number: ");
while (!in.hasNextInt()) {
String input = in.next();
System.out.printf(""%s" is not a valid number. ", input);
}
number = in.nextInt();
} while (number < 0);

System.out.printf("You have entered a positive number %d. ", number);
product=computeMultiplication(number);
System.out.printf("Product is: %d ", product);

}else{
product=computeMultiplication(number);
System.out.printf("Product is: %d ", product);
}

}

};

...............................................................................................................................