Debug the program so it prints the factorial of a positive integer entered by the user. This is calculated by multiplying all the numbers from 1 up to that number together. For example, 5 factorial is 5*4*3*2*1 = 120.
import java.util.Scanner;

public class U4_L1_5_Activity_Two{
public static void main(String[] args){

Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
while(num >= 0){
int prod = 1;

prod = prod*num;
System.out.print(prod);
num--;
}


}
}

Respuesta :

Your main problem was declaring int prod in the while loop. This caused prod to be reset back to 1 every time the while loop ran.

Ver imagen Cytokine

Debugging a program involves locating and fixing the errors in a program.

The syntax of the given program is correct; this means that, the program will run without a hitch; however, the program will not give the expected output

The errors in the program are as follows:

  • Placing int prod = 1; and System.out.print(prod) in the while loop
  • Incorrect while statement

The fix to these errors are:

  • Placing int prod = 1; and System.out.print(prod) outside the while loop
  • Correct the while statement

So, the correct program is as follows:

import java.util.Scanner;

public class U4_L1_5_Activity_Two{

public static void main(String[] args){

   Scanner scan = new Scanner(System.in);

   int num = scan.nextInt();

   int prod = 1;

   while(num > 0){

       prod = prod*num;

       num--;

   }

System.out.print(prod);

}

}

The texts in italics represent the fixes

Read more about debugging at:

https://brainly.com/question/23527739