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

Discussion Overview

The discussion revolves around computing the factorial of a number N in C++. Participants share code snippets and suggest improvements, focusing on program structure, input validation, and computational limits.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant suggests initializing a variable totalValue to N and using a loop to compute the factorial.
  • Another participant provides a code snippet that includes user input and a while loop to calculate the factorial, but notes the absence of input validation.
  • A third participant points out that the while loop could be simplified by stopping at 2, as multiplying by 1 is unnecessary.
  • This participant also mentions that the program only works correctly for values up to 12! and suggests implementing a bounds check for inputs between 1 and 12.
  • There is a mention that the complexity increases significantly for values beyond 20! without further elaboration.

Areas of Agreement / Disagreement

Participants express varying opinions on the implementation details, particularly regarding input validation and computational limits, indicating that multiple views remain on the best approach to handle larger values of N.

Contextual Notes

Limitations include the lack of input validation in the provided code snippets and the potential for integer overflow when calculating large factorials, particularly beyond 12! or 20!.

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