How do I compute N factorial in C++?

  • C/C++
  • Thread starter carl123
  • Start date
  • Tags
    C++ Program
In summary, this conversation discussed the steps to create a program that calculates the factorial of a user-inputted integer. The program uses a while loop to count down from the inputted integer to 1 and update the total value, which is then outputted. It was also mentioned that the program should have a check for invalid input and a bounds check for integers from 1 to 12. Going beyond 20 would require a more complicated program.
  • #1
carl123
56
0
Write a program that let's a user enter N and that outputs N! (meaning N*(N-1)*(N-2)*...*2*1). Hint: Initialize a variable totalValue to N, and use a loop variable i that counts from N-1 down to 1.

#include <iostream>
using namespace std;

int main() {
int totalVal = 0;
int userInt = 0;

// FIXME: Ask user to input an integer, store in userInt

totalVal = userInt;
// FIXME: Add while loop that counts down to 1, updating totalVal

cout << userInt <<"! is " << totalVal << endl;

return 0;
}
 
Technology news on Phys.org
  • #2
#include <iostream>
using namespace std;

// If no checks for invalid input

int main() {
int totalVal = 0;
int userInt = 0;

// FIXME: Ask user to input an integer, store in userInt
cout << ("Enter integer value > 0") <<endl;
cin >> userInt;

totalVal = userInt;
// FIXME: Add while loop that counts down to 1, updating totalVal

int temp = userInt;
while(temp>1)
{
totalVal*= --temp;

}

cout << userInt <<"! is " << totalVal << endl;

return 0;
}
 
  • #3
The while loop can be
while(temp > 2);
No point in multiplying by 1.

The program only works up to 12! Probably should be a bounds check for an integer from 1 to 12.
It gets a lot more complicated if you want it to go beyond 20!
 
  • Like
Likes Conn_coord
  • #4
I just completed the program. Did not write new :)
 

Related to How do I compute N factorial in C++?

1. What is a "C++ Program to Compute N"?

A "C++ Program to Compute N" is a computer program written in the C++ programming language that is designed to calculate a given value N. This value can be any number, and the program will perform mathematical operations to compute the final result.

2. How does a "C++ Program to Compute N" work?

A "C++ Program to Compute N" works by taking in a value for N and using various mathematical operations, such as addition, subtraction, multiplication, and division, to calculate the final result. The program follows a specific set of instructions, or algorithm, to perform these calculations.

3. What is the purpose of a "C++ Program to Compute N"?

The purpose of a "C++ Program to Compute N" is to provide a reliable and efficient way to calculate a given value N. This can be useful in various fields, such as engineering, finance, and science, where precise calculations are crucial.

4. Can a "C++ Program to Compute N" be modified to compute other values?

Yes, a "C++ Program to Compute N" can be modified to compute other values by editing the input and output variables and changing the mathematical operations used. The program's algorithm may also need to be altered to accommodate the new calculations.

5. Is a "C++ Program to Compute N" difficult to learn and use?

The difficulty of learning and using a "C++ Program to Compute N" depends on the individual's level of programming experience. For those who are familiar with the C++ language, it may be relatively easy to understand and use. However, for beginners, it may require more time and practice to grasp the concepts and successfully implement the program.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
900
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
3
Views
747
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top