New course! Every coder should learn Generative AI!
Try a free lesson+ 4
what is the problem of this code?
hi i was doing some code in my program (c++) and i faced an error. the error is not from compiler, it's from my program itself. 1. the program is busy with something because it output the result VERY SLOWLY. 2. i want it to calculate the factorial of the giving number but its result is wrong. (input: 5, output: 5! + 0! + 0! .... = 0) any solutions? sorry about my language if i write anything wrong. https://code.sololearn.com/cZ91XcJHZZ16/?ref=app
4 Answers
+ 7
"sorry about my language"
Your language is perfect! 8D
Comment out line 9 `#include <conio.h>` and 29 `getch();`
And reconstruct the code like so
#include <iostream>
//#include <conio.h>
using namespace std;
int main () {
int factNum,
factorial,
result = 1;
cout << "Enter a number : ";
cin >> factNum;
factorial = factNum;
while (factorial) {
if (factorial == 1)
cout << "1 = " << result << endl;
else {
cout << factorial << "! + ";
result *= factorial;
}
--factorial;
}
cout << "Enter a Key to exit" << endl;
//getch();
return 0;
}
+ 4
Hope This will help you
https://code.sololearn.com/ct5pqvsJzYSW/?ref=app
+ 3
I modified it, it now works. See it here:
https://code.sololearn.com/cspiHYNbres3/#
+ 2
what's the logic of this code ?
i don't get why this code is such crappy !
there are simple codes for calculating the factorial of small numbers.
your code won't give you only a number as an output
your output will be like :
n!+(n-1)!+..+2!+1=n!
why including <conio.h> and using getch() at the end? i don't think they're needed!