+ 2

i have made factorial program in c++ but it is not running

initially i thought that it is due to data type but even after changing data type to long long it is not working #include <iostream> using namespace std; long long factorial(int x); int main() { cout << factorial(3); return 0; } // method for factorial long long factorial(int number) { long long num = 1; while(number>1) { num *=number; }number--; return num; } i just want to clarify that program has compiled but it not printing result.I am using cmd for running it.

9th Sep 2017, 4:28 AM
shobhit
shobhit - avatar
3 odpowiedzi
+ 7
Check the decrementing condition you put after while loop, number-- should come inside while loop. At present your loop is running infinite times for the value number=3
9th Sep 2017, 4:35 AM
Sachin Artani
Sachin Artani - avatar
+ 5
@Shobit: A little advice, you should write a pseudocode first, then convert it into actual code. Pseudo code will help you to resolve bugs. i.e say the code that you have written is not showing desired output.So read your code and follow it, convert it into logic. this will help you fix where things are going wrong. Not only this, it will also help you to enhance your understanding and debugging skills. here is the link that explain pseudocode. https://en.m.wikipedia.org/wiki/Pseudocode
9th Sep 2017, 5:21 AM
Popat
Popat - avatar
+ 1
thnx for ur concern @newbie but i think its mainly because i have migrated from java to c++.
10th Sep 2017, 5:13 AM
shobhit
shobhit - avatar