Question

5. (10 points) The factorial of a nonnegative integer n is written n! and is defined as follows. n 2) ..1 (for values of n gr

1 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
System.out.println("Enter non negative integer: ");
int n=in.nextInt();
long f = 1;
for(int i = 1; i <= n; ++i)
{
// factorial = factorial * i;
f *= i;
}
System.out.println("Factorial is: "+f);
}
}

Enter non negative integer: 5 Factorial is: 120

Add a comment
Know the answer?
Add Answer to:
5. (10 points) The factorial of a nonnegative integer n is written n! and is defined...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The factorial of a nonnegative n written as n! is defined as follows: n!= n*(n-1)*(n-2) *...

    The factorial of a nonnegative n written as n! is defined as follows: n!= n*(n-1)*(n-2) * .... *1 (for all values of n greater than 0) and 0! =1. For example 5! = 5*4*3*2*1 which is 120. (can also be 1*2*3*4*5) Write a C++ program that reads a nonnegative integer and computes and prints its factorial.

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1)...

    If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1) * (n-2) * ( n-3)… * By convention, 0! = 1. You must write a program that allows a user to enter an integer between 1 and 7. Your program must then compute the factorial of the number entered by the user. Your solution MUST actually perform a computation (i.e., you may not simply print “5040” to the screen as a literal value if...

  • Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return...

    Given algorithm- procedure factorial (n: nonnegative integer) if n = 0 then return 1 else return n*factorial(n-1) {output is n!} Trace the above algorithm when it is given n = 7 as input. That is, show all steps used by above algorithm to find 7!

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Question 6 of 19 Python The factorial of a positive integer n, fact(n), is defined recursively...

    Question 6 of 19 Python The factorial of a positive integer n, fact(n), is defined recursively as follows: fact(n) 51, when n51 fact(n) 5n * fact(n21), otherwise Define a recursive function fact that returns the factorial of a given positive integer.

  • Written in expanded form, the usual factorial function is n! = n middot (n - 1)...

    Written in expanded form, the usual factorial function is n! = n middot (n - 1) middot (n - 2) ... 3 middot 2 middot 2 middot 1. The difference between elements in the product is always 1. It can be writ in recursive form as n! = n middot (n - 1)! (e.g., 10! = 10 middot 9!, 23! * 23 middot 22!, 4! = 4 3!, etc.). The purpose of this problem is to generalize the factorial function...

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

  • In Java Write a method factorial that accepts an integer parameter n and that uses recursion...

    In Java Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below. Call Output factorial(0); 1 factorial(1); 1 factorial(3); 6 factorial(5); 120 factorial(10); 3628800 factorial(-4); IllegalArgumentException

  • Write a program that reads an integer greater or equal to 2, n, and prints a...

    Write a program that reads an integer greater or equal to 2, n, and prints a shape of a nline hollow inverted pyramid of stars. Your program should interact with the user exactly as it shows in the following two executions: Execution example 1: Please enter an integer, greater or equal to 2: 5 ********* -*----- * --*--- * ---*--* ----* Execution example 2: Please enter an integer, greater or equal to 2: 3 ***** -* * --*

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT