Answer to Question #283746 in C++ for Collay

Question #283746

2. Factorials

by CodeChum Admin

For those of you who may not now, a factorial is a product of multiplying from 1 until the number. Now, you must make a program that will accept an integer and then print out its factorial using loops.


Are you up for this task?



Instructions:

  1. Input a random positive integer.
  2. Using a for() loop, generate the factorial value of the integer and print out the result.Tip: Factorials work like this: Factorial of 5 = 1*2*3*4*5
  3. Note that there is a special case and that is the factorial of 0 which is 1.

Input

A line containing an integer.

5

Output

A line containing an integer.

120
1
Expert's answer
2022-01-02T02:23:32-0500
#include <iostream>
using namespace std;


int main() {
	int n;
	long double factorial = 1.0;




	cin >> n;


	if (n < 0)
		cout << "Error! Factorial of a negative number doesn't exist.";
	else {
		for(int i = 1; i <= n; ++i) {
			factorial *= i;
		}
		cout << factorial;    
	}




	cin>>n;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog