Question

In: Computer Science

In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...

In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2 * 1 = 6. Then 6 will be displayed.

Output: Enter a number: 4

4! = 24

Would you like to try another number? Enter y - yes n

Solutions

Expert Solution

Required Program in C++ -->

#include <iostream>
using namespace std;
int main()
{
int i,fact,number; // declare variables in integer
char another; // declare variable in character
int choice=1; // declare choice and initialize it with 1
while(choice){ // while choice is 1
fact=1; // initialize fact with 1
cout<<"Enter any Number: "; // print enter any number
cin>>number; // read input from user
for(i=1;i<=number;i++){ // for loop will start from i=1 to i=number
fact=fact*i; // fact is multiplied by i
}
cout<<number<<"! = "<<fact<<endl; // print number"! = " fact
cout<<"Would you like to try another number"; // ask user if want to try again ?
cin>>another; // read input from user
if(another == 'Y'|| another =='y'){ //if user enter y or Y
choice=1; // then choice =1, and while loop will continue
}
else if(another =='N'||another =='n'){ // if user enter n or N
choice=0; // then choice=0, and while loop ends
}
}
return 0;
}


Related Solutions

The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
The factorial of an integer ? is defined using the following equation: ?! = ? ∗...
The factorial of an integer ? is defined using the following equation: ?! = ? ∗ (? − 1) ∗ (? − 2) ∗ … ∗ 2 ∗ 1 In combinatorics, permutations is the number of ways of selecting a subset of some elements such that the order matters. For example, there are 6 permutations of letters A, B, and C. A B C ,A C B, B A C, B C A, C A B, C B A, The...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n...
prove or disprove .if n is a non negative integer, then 5 divides 2 ⋅ 4^n + 3⋅9^n.
8. Definition: A set A is finite if there exists a non-negative integer c such that...
8. Definition: A set A is finite if there exists a non-negative integer c such that there exists a bijection from A to {n ∈ N : n ≤ c}. (The integer c is called the cardinality of A.) (a) Let A be a finite set, and let B be a subset of A. Prove that B is finite. (Hint: induction on |A|. Note that our proof can’t use induction on |B|, or indeed refer to “the number of elements...
Use a direct proof to prove that 6 divides (n^3)-n whenever n is a non-negative integer.
Use a direct proof to prove that 6 divides (n^3)-n whenever n is a non-negative integer.
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
write a c++ program an expression that determines if an integer, n is a negative four...
write a c++ program an expression that determines if an integer, n is a negative four digit number. write a c++ program an expression that determines if a string, wd, equals "so" ignoring case.
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT