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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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); //
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply