C++ task about factorial | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ task about factorial

Complies a program that imports N natural number and outputs YES if this number is a factorial number and NO otherwise I have a question Can anybody solve this task without arrays recursion and function #include <iostream> using namespace std; int main() { int number, temp; cin>>number; if(number<=0){ cout<<"error"; return 0; } int counter=1, sum=0; temp=number; while (counter<temp){ if(temp%counter==0){ sum*=counter; counter++;} else{ cout<<"no"; } } if (number==sum) cout<<"yes"; return 0; } I don't know I am in a right way

15th Jan 2019, 7:27 PM
Arsen
8 Answers
+ 2
1. Initial value of "sum" shouldn't be 0 as sum*=counter wouldn't have any effect on it. Try sum=1. 2. You can simplify your program by changing the while loop to: while (number%counter==0){ sum*=counter; counter++;}
15th Jan 2019, 9:48 PM
Diego
Diego - avatar
+ 2
Can you please paste your code in the Playground and share the link? It'd make it easier for people to help you.
15th Jan 2019, 10:44 PM
Diego
Diego - avatar
16th Jan 2019, 10:39 AM
Zen
Zen - avatar
0
As Diego said, also use code feature that you got here on SoloLearn so it will be easier to solve your problem
15th Jan 2019, 10:26 PM
Edin Hajdarevic
Edin Hajdarevic - avatar
0
Diego I try with sum=1 And changed the loop but it doesn't work
15th Jan 2019, 10:37 PM
Arsen
0
Sorry for so much late I answered your question but in my country was night so I went to sleep
16th Jan 2019, 9:35 AM
Arsen
0
Zen thanks for helping me It works
16th Jan 2019, 11:47 AM
Arsen