How do I compute N factorial in C++?

  • Context: C/C++ 
  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary
SUMMARY

This discussion focuses on computing the factorial of a number N in C++. The provided code initializes a variable `totalVal` to store the factorial result and uses a while loop to multiply values from N down to 1. The implementation includes user input handling and suggests a bounds check for values between 1 and 12, as the program only accurately computes factorials up to 12!. For larger values, such as 20!, the complexity increases significantly.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with loops, specifically while loops
  • Basic knowledge of user input handling in C++
  • Concept of factorial and its mathematical implications
NEXT STEPS
  • Implement error handling for invalid user input in C++
  • Explore the use of recursion for computing factorials in C++
  • Learn about data types in C++ to handle larger factorials, such as using `long long`
  • Research libraries or algorithms for computing large factorials beyond 20!
USEFUL FOR

C++ developers, computer science students, and anyone interested in algorithm optimization for mathematical computations.

carl123
Messages
55
Reaction score
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

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

return 0;
}
 
Technology news on Phys.org
#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
count << ("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;

}

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

return 0;
}
 
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   Reactions: Conn_coord
I just completed the program. Did not write new :)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K