0

can you check my cpp code?

I make a cpp code which finding trailing zeros of factorial. For example, 5! is 120. So, Result is 1. Also, 6! is 720. Result needs to be 1. But in my computer, I don't have compiler to launch this code. So, i can't convince that i make right code. If you not mind launching, plz compile this code and tell me what is error in my code. #include <iostream> using namespace std; int main() { int n; //number of factorial seed cout<<"type number"<<endl; cin>>n; int x[n]; // make n array int m5[n]; // make n array of quotient devided by 5 int m2[n]; // make n array of quotient devided by 2 int counter=0; //component of m5 array int counter1=0; //component of m2 array int r=0; int sum=0; int sum1=0; for(int i=0;i<500;i++){ x[i]=i+1; r=i+1; while(r%5==0){ counter++; } while(r%2==0){ counter1++ } m5[i]=counter; m2[i]=counter1; } for(int j=0;j<n;j++){ sum+=m5[j]; //sum of component in m5 array sum1+=m2[j]; //sum of component in m2 array } if(sum<sum1){ cout<<"number of trailing zero is"<<sum<<endl;} else{ cout<<"number of trailing zero is"<<sum1<<endl; } return 0; }

10th Apr 2018, 11:36 AM
이병주
이병주 - avatar
12 odpowiedzi
0
If you mean IDE when you say "compiler program" you can use Codeblocks if you don't want to use visual studio. Or you use a text editor, get a compiler and compile it yourself. For smaller sandboxing I usually use notepad++ and compile it with a nppexec script. But I recommend you to just download Codeblocks. It's lightweight and quite easy to use as a beginner. Or you Google for an IDE guide and choose what you like the most.
10th Apr 2018, 2:23 PM
Alex
Alex - avatar
+ 1
you got syntax error
10th Apr 2018, 11:44 AM
Rasul Kamolov
Rasul Kamolov - avatar
0
what's syntax problem in my code?
10th Apr 2018, 12:20 PM
이병주
이병주 - avatar
0
You can't initialize an array like this at runtime. Either use malloc or (better) use vectors. But another question: why you don't have a compiler on your computer?
10th Apr 2018, 1:23 PM
Alex
Alex - avatar
0
i don't install visual studio or other compile program in my computer. could you tell me compiler program instead of visual studio? other question: then,if i change x[n] and m5[n] to x[500] and m5[500], output will be resulted?
10th Apr 2018, 1:57 PM
이병주
이병주 - avatar
0
And no, there won't be an output. There is still a ; missing. But even with the syntax fixed it output's nothing, because you get trapped in an infinite loop pretty soon. With r=2 you start this while loop: while(r%2==0){ counter++; } Maybe you meant if(r%2 == 0)?
10th Apr 2018, 2:31 PM
Alex
Alex - avatar
0
Here is your code. You can test it yourself: https://code.sololearn.com/ceVfHvRC6hjv/?ref=app
10th Apr 2018, 2:34 PM
Alex
Alex - avatar
0
thank you for your help. what i want to do is continue loop until remainder is not 0. And 1 should be plused to counter until condition of loop doesn't work.(i.e. if r%5 !=0, then output counter must be how many it goes through loop)
10th Apr 2018, 2:48 PM
이병주
이병주 - avatar
0
I actually don't know how this is supposed to output something useful. There is nothing that has to do with factorials. What are your complete thoughts about the algorithm?
10th Apr 2018, 2:55 PM
Alex
Alex - avatar
0
in fact,i don't want to calculate of n! i want to know how many trailing 0 in n!. So i think an array and each array component is 1,2,3,4....n for x[i](i.e. x[0]=1,x[1]=2....). Components must devided it by 5 until remainder isn't 0. And how many it devide will be component of quontient array. for example, if n is 5, array will be x[0]=1,x[1]=2,x[2]=3,x[3]=4,x[4]=5. and quintient array is m5[0]=1/5=0,m5[1]=0,m5[2]=0,m5[3]=0, m5[4]=1. Finally, sum of components in m5 array is number of trailing zero.
10th Apr 2018, 3:10 PM
이병주
이병주 - avatar
0
So, i think whileloop can help the condition r%5==0 and put counter++ ; in loop to check how many loop works.
10th Apr 2018, 3:21 PM
이병주
이병주 - avatar
- 3
Make->made Which finding->which finds And other several mistakes... I suggest asking a person living your country.
10th Apr 2018, 3:00 PM
syul
syul - avatar