Last digit of the factorial of an integer

Hi. I am trying to make a program which first reads the number of times the program should be able to process my calculations and then processes it and outputs all the results at the end.
I dont understand why the output of the code is 0
Here is my code:
#include <iostream>

using namespace std;

int main()
{
int casos;//casos is the times I want to introduce integers
cin>>casos;
int num;
int output[casos];
//Process
for(int q=0;q<casos;q++){
cin>>num;
for(int n=num-1;n>0;n--){
num=num*n;
}
while(num!=0){
num=num/10;
}

num%10;
output[q]=num;
}
//Cout all the results
for(int x=0;x<casos;x++){
cout<<output[x]<<endl;
}
return 0;
}
Maybe someone knows how to help me?
Thanks
without trying to unravel your unformatted and odd code,
math says multiply by 5 and by 2 is the same as multiplying by 10. So, after 5, all factorials end in zero, and in short order, they end in multiple zeros.
does it give something other than zero for {2,3,4} ?
maybe print the factorial itself, first, to see if its working, then you can play with the digits.
Last edited on
And just to point out the obvious, this line:
num%10;
doesn't do anything (does not change the state of the program in any meaningful way).
Last edited on
Slightly less obvious is that the while loop divides num down to 0, so it will always be 0 after that.
The code will be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
int casos;//casos is the times I want to introduce integers
cin>>casos;
int num;
int output[casos];
//Process
for(int q=0;q<casos;q++)
{
  cin>>num;
  for(int n=num-1;n>0;n--)
  {
    num=num*n;
  }

  num = num%10;
  output[q]=num;
}
//Cout all the results
for(int x=0;x<casos;x++){
cout<<output[x]<<endl;
}
return 0;
}


To learn more about finding factorial of a number, please visit:
https://www.alphacodingskills.com/cpp/pages/cpp-program-find-factorial-of-a-number.php
if you just want a factorial, there are a fairly small # you can have in 64 bits, and a lookup table is usually good enough for what you want to do in practice, rather than compute them, esp for things where you mostly use just a few small ones like statistics.
Topic archived. No new replies allowed.