Question

Compute factorial of a number Factorial of n = 1*2*3 *n 702

In C++ using the while loop..

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

#include<iostream>
using namespace std;

int main()
{
int n;
int fact=1;
cout<<"Enter number ";
cin>>n;
if (n<0)
{
cout<<"Enter positive value";
}
else
{
while(n>0)
{
fact=fact*n;
n--;
}


}
cout<<"The factorial of "<<n<<" is "<<fact;
return 0;
}

nter number4 The factorial of 0is 24 Process returned 0 〈0x0〉 execution time : 1.532 s Press any key to continue

Add a comment
Know the answer?
Add Answer to:
In C++ using the while loop.. Compute factorial of a number Factorial of n = 1*2*3...
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
  • Write a C++ function to find factorial of an integer number N >0. Using the factorial...

    Write a C++ function to find factorial of an integer number N >0. Using the factorial function print the factorial of two inputs as shown below: Sample input: 1 4 Expected output: 1 24

  • C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial...

    C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial of a number. The factorial of a number is given by n! = 1*2* ... *n So 1! = 1, 2! = 1*2, 3! = 1*2*3 ... For example: Test Result printf("%llu\n", factorial(5)); 120 printf("%lu\n", factorial(20)); 2432902008176640000

  • Programming language: C NOT c++ Problem 2 (20 points) Both the for loop and the do-while...

    Programming language: C NOT c++ Problem 2 (20 points) Both the for loop and the do-while loop can be transformed into a simple while loop. For each of the following examples a) and b), write equivalent code using a while loop instead. double rand_double)( double ret - (double)rand(); return ret/ (RAND MAX 1); int factorial (int n) int for i, ret =1; (i=2; i <= ret *= 1; n; i++) int sample_geometric_rv(double p)l double q return ret; int ne; do...

  • Calculate ? using the approximation n! n ! is the factorial: n!-1 × 2 × 3...

    Calculate ? using the approximation n! n ! is the factorial: n!-1 × 2 × 3 × × n. |1×3 x-..×n, nis odd n!! is the semifactorial: n!!-12 x 4 x x n, n is even not to be confused with (n!)!, which is the factorial function iterated twice. The semifactorial of n is the product of the integers between 1 and n which have the same parity (even or odd) as n. For example, 7!! 1 × 3 ×...

  • Calculating the Factorial of a Number - PSEUDOCODE In mathematics, the notation n! represents the factorial...

    Calculating the Factorial of a Number - PSEUDOCODE In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of all the nonnegative integers from 1 up through n. For example: 7! = 1 × 2 × 3 × 4 × 5 × 6 × 7 = 5,040 and 4! = 1 × 2 × 3 × 4 = 24 Design a program that asks the user to enter a nonnegative...

  • C++ //Write prototype for function factorial that accepts an int num and returns an int /*...

    C++ //Write prototype for function factorial that accepts an int num and returns an int /* WITH LOOP OF YOUR CHOICE: Write code for function factorial that accepts an int num and returns the num's factorial factorial(5); 1*2*3*4*5 returns 120 DON'T FORGET TO WRITE TEST CASE. */ //write includes statements //write using statements for cin and cout /* Use a do while loop to prompt the user for a number, call the factorial function, and display the number's factorial. Also,...

  • Special Problem: Automated Factorial Create a MATLAB function called xfact that uses a loop to calculate...

    Special Problem: Automated Factorial Create a MATLAB function called xfact that uses a loop to calculate the factorial of an integer. Your function should take a single integer input. Your function should compute the factorials of every integer from 1 to the integer provided by the input. Test your function by prompting the user to enter an integer N and, using a loop create an array named X of length N that contains the factorials of the numbers 1 to...

  • Write a JavaScript function that asks the user for a number, n, then calculates the factorial...

    Write a JavaScript function that asks the user for a number, n, then calculates the factorial for that number. You must use a loop to find the factorial.

  • C++ 3. Write a program that recursively calculates n factorial (n!) a) Main should handle all...

    C++ 3. Write a program that recursively calculates n factorial (n!) a) Main should handle all input and output b) Create a function that accepts a number n and returns n factorial c) Demonstrate the function with sample input from console. n! n * n-1 * n-2 * n-3, for all n > 0 For example, 3!-3 21-6 using a recursive process to do so. Example output (input in bold italics) Enter a number: 5 5120

  •        Write a shell script “6-1.sh” to calculate the exponentiation using while loop. This script needs...

           Write a shell script “6-1.sh” to calculate the exponentiation using while loop. This script needs to take two inputs like “6-1.sh a b”, and outputs the result of “ab”. Note: You need to implement it, DO NOT use the exponentiation operation provided by the Linux.        Write a shell script “6-2.sh” to calculate the factorial of a number using while loop. This script needs to take one input like “6-2.sh a”, and outputs the result of “a!”.

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