Question

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.

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

Program:

#include <iostream>

using namespace std;

int main()
{
int num,fact=1,i ;
char *process;
  
cout<<"Enter a non-negative number: ";
cin>>num;
if(num>0)
{
for(i=num;i>=1;i--)
{
fact=fact*i;
}
cout<<"Factorial of the "<<num<<" is "<<fact;
}
else
{
cout<<"Please enter a non-negative(positive) number!!";
}
return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
The factorial of a nonnegative n written as n! is defined as follows: n!= n*(n-1)*(n-2) *...
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
  • 5. (10 points) The factorial of a nonnegative integer n is written n! and is defined...

    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 greater than 1) nn (n-l) and n-# 1 (for n 0 or n-1) l. Write a program that reads a nonnegative integer and computes and prints its factoria

  • 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...

  • Done in C++ using visual studio Instructions to solve all three problems below using function •...

    Done in C++ using visual studio Instructions to solve all three problems below using function • The value of n should be asked from user in the main() and pass the value of n to an additional function as argument value; Ex: Factorial(n). • The functions should calculate the values according to the formulas below and return the values. • Then the main() function should print out those returned values. • All three parts should have their own separate function,...

  • 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...

  • 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...

  • 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...

  • 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

  • The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS...

    The cosine function is analytically defined as follows: m 1. x² + x6 (-1)" x2m COS X = (-1)" x2n (2n)! 2!*4!- 6 + ... + (2m)! Write a complete C program that has three functions: main, cosine, and factorial. The cosine function receives a real number x and returns a real number representing the cosine of x. The cosine function also receives another integer m that determines the number of terms that will be used in computing the cosine...

  • 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!

  • 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