I don't know why this c++ factorial code isn't working | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

I don't know why this c++ factorial code isn't working

#include <iostream> using namespace std; int main() { int n,i; cout<<"Please enter the factorial..."<<endl; cin>>n; i=1; while (i<=n) n=n*i; i++; cout<<"The factorial = "<<n; return 0; }

15th Feb 2017, 2:05 PM
Mahmoud
2 Réponses
+ 6
@Mahmoud This is not python , {} is mandatory :D
15th Feb 2017, 2:21 PM
Mr.Robot
Mr.Robot - avatar
+ 2
#include <iostream> using namespace std; int main() { int n,i,fact; cout<<"Please enter the factorial..."<<endl; cin>>n; i=1; while (i<=n) { fact*=i; i++; } cout<<"The factorial = "<<fact; return 0; } Change log braces after loop you were updating n itself so the loop had must become endless.
15th Feb 2017, 2:12 PM
Megatron
Megatron - avatar