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
8 ответов
+ 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++;}
+ 2
Can you please paste your code in the Playground and share the link? It'd make it easier for people to help you.
+ 2
Here you go:
https://code.sololearn.com/co3W3l7SEZ0H/#cpp
0
As Diego said, also use code feature that you got here on SoloLearn so it will be easier to solve your problem
0
Diego I try with sum=1
And changed the loop but it doesn't work
0
Sorry for so much late I answered your question but in my country was night so I went to sleep
0
Zen thanks for helping me
It works