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

You must select, copy and paste the output created by your program You must uplo

ID: 3729542 • Letter: Y

Question

You must select, copy and paste the output created by your program

You must upload your source code file to the Assignment submission online

Make sure your program has comments

You must follow the coding conventions and indent your code before submitting otherwise you automatically lose 2 points.

If you submit your source code together with something that is not a true output of the run of that source code, this will be seen as academic dishonesty and you will receive a 0 with no chance to resubmit

Objectives:       

Write a subroutine that calls itself

Problem

Write a subroutine called factorial that takes a positive number as parameter and returns the factorial of that number. Here is the program that will test your factorial subroutine:

class TestFactorial {

   public static void main(String args []) {

      System.out.println( "7! = " + UsefulRoutines.factorial(7));

      System.out.println( "10! = " + UsefulRoutines.factorial(10));

      System.out.println( "0! = " + UsefulRoutines.factorial(0));

   }

}

/******** OUTPUT **********

7! = 5040

10! = 3628800

0! = 1

***************************/

As you can see from the test program above, your factorial() subroutine will be inside class UsefulRoutines. You can leave the other two subroutines (spellDigit() and spellNumber()) inside the class UsefulRoutines, even though you will not be using them this week.

Notes:

• You MUST use recursion to solve this problem

• This program will have no user input.

• The subroutine factorial() will work for any positive integer.

• The subrouting factorial() will not contain a call to System.out.println().

• You must submit both source code files (TestFactorial.java and UsefulRoutines.java)

Explanation / Answer

public class UsefulRoutines {

  

   // Method to find factorial of given number

public static int factorial(int n)

{

int res = 1, i;

for (i=2; i<=n; i++)

res *= i;

return res;

}

}

#########

public class TestFactorial {

   public static void main(String args []) {

       System.out.println( "7! = " + UsefulRoutines.factorial(7));

       System.out.println( "10! = " + UsefulRoutines.factorial(10));

       System.out.println( "0! = " + UsefulRoutines.factorial(0));

   }

}

/*

Sample run:

7! = 5040

10! = 3628800

0! = 1

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote