Ask Question
10 May, 23:49

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 5!=5 * 4 * 3 * 2 * 1 = 120 Write a programn (in Java) to print and calculate the factorial for a given (n) number

+3
Answers (1)
  1. 11 May, 02:53
    0
    I'll show a few implementations, starting with the easiest to understand.

    1.) While loop.

    public static void main (String[] args) {

    System. out. print (factorial (n));

    }

    public static int factorial (String n) {

    int result = 1;

    while (n > 0) {

    result * = n;

    n--;

    }

    return result;

    }

    2.) For loop (I'll just show the contents of the factorial (String) method):

    int result = 1;

    for (int i = n; i > 0; i--) {

    result * = i;

    }

    return result;

    3.) Recursively.

    public static int factorial (int n) {

    if (n = = 1) return n;

    return n * factorial (n-1);

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers