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

• Write a recursive method with method header int factorial(int n) to calculate

ID: 3631786 • Letter: #

Question

• Write a recursive method with method header int factorial(int n) to calculate the factorial of a number.
? factorial(…) should check to make sure that n is a positive integer greater than or equal to 0 – if not, it should print out an error message and use System.exit(0) to exit the program.
? The factorial of a number n, often written n!, is n × n-1 × n-2 × … × 2 × 1. By definition 0! == 1.
? If n is 0 or 1, factorial(…) should return 1, otherwise it should return n × factorial(n-1).

• Write a main method that:
? creates a Scanner object, call it input
? prints out a prompt to tell the user to enter the next positive integer that they want to see the factorial of, and to enter any negative number when they are done
? with an appropriate loop, calls factorial(…) on each positive number the user enters and prints out a message like this:
n factorial is n!
where n is replaced by the number they entered, and n! is replaced by its factorial, the number returned by calling factorial(n).

Explanation / Answer

please rate - thanks

import java.util.*;
class recursivefactorial
{public static void main(String[] args)
   {int num;
    Scanner input=new Scanner(System.in);
    System.out.print("Enter a a positive number (negative when done): ");
    num=input.nextInt();
    while(num>=0)
         {System.out.println(num+"! = "+factorial(num));
           System.out.print("Enter a a positive number (negative when done): ");
          num=input.nextInt();
        }
    System.exit(0);
}
public static int factorial(int n)
{if(n==0)
    return 1;
else
     return (n*factorial(n-1));

}
}