Page 1 of 1

I NEED FIX THIS PROGRAM, TY import java.util.Scanner; public class Recursion { //This is a opt

Posted: Mon Jun 06, 2022 6:22 pm
by answerhappygod
I NEED FIX THIS PROGRAM, TY
import java.util.Scanner;

public class Recursion {
//This is a optional
program.
public static void main(String[] args) {
Scanner keyboard = new
Scanner(System.in); //Input
int selection; //varibles
int n;
int result;
do
{
System.out.println("Menu");
//Print out
System.out.println("Program 1");

System.out.println("Exit");
System.out.print("> ");
selection = keyboard.nextInt();
if (1 == selection) {

//Conditional, if input = 1, to the
Calculate factorial


// if input = 2,
Exit the program.
System.out.println("Enter an
integer");
System.out.print("> ");
n = keyboard.nextInt();
result = factorial(n);
System.out.println("The factorial of "
+ n + " is: " +
result);
} else if (2 == selection) {
System.out.println("Goodbye!");
}
else {
System.out.println("Incorrect
input!");
}
} while(2 != selection);
keyboard.close();
}
public static int
factorial(int n) //if input neigher 1
nor 2, error, return to the input section.
{
if (n == 0
|| n == 1)

return 1;

return n *
factorial(n - 1); //
}
}