Write a recursive method to compute the factorial of a number.

What will be an ideal response?

```
public static int factorial(int n)
{
if(n < 0)
{
System.out.println(“Sorry negative numbers not allowed.”);
System.exit(0);
}
if(n == 1) //base case
return(1);
else
return (n * factorial(n-1));
}

```

Computer Science & Information Technology

You might also like to view...

New to Word 2013 is one-click row and column insertion, which enables you to add a new row or column by clicking the ________ control

A) asterisk B) plus sign C) arrow D) diamond

Computer Science & Information Technology

Answer the following statements true (T) or false (F)

1. Any sorting algorithm, such as bubble sort or selection sort, that can be used on data stored in an array can also be used on data stored in a vector. 2. If algorithm A requires 2n + 1 basic operations to process an input of size n, and Algorithm B requires 3n + 2 basic operations to process the same input, algorithms A and B are considered to be equally efficient. 3. With pointer variables you can access, but you cannot modify, data in other variables. 4. An array name is a pointer constant because the address it represents cannot be changed during run-time. 5. Memory cannot be allocated after a program is already running.

Computer Science & Information Technology