Stuck on problem named "Small Factorial"

FLOW018 Problem - CodeChef : problem statement.
CodeChef: Practical coding for everyone : link to my solution.
Can anyone tell me why am I getting wrong answer? I checked the code many times, but it wasn’t helpful.

https://www.codechef.com/viewsolution/48370915

    for(int y = 0  ; y < (n-1) ; y++){
        
        n--;
        fact_num = fact_num*n;
        
    }

isn’t doing what you think it is doing, you’re decrementing n and incrementing y at the same time. Also you don’t need an array to store the results. I guess after checking the code down below, you’ll probably understand what you’ve done wrong (The solution is in C because I don’t like C++).

#include<stdio.h>
int main(){
    int t;
    scanf("%d",&t);
    long n;
    long res;
    while(t--){
    res=1;
    scanf("%ld",&n);
    if(n!=0){
        for(;n>0;n--){
            res*=n;
        }
    }
    else{
        res=1;
    }
    printf("%ld\n",res);
    }
}
1 Like

output is not correct for
input
3
3
4
5
it give->
6
24
60
correct is->
6
24
120

1 Like

You can do this . it works for me

t = int(input())
n = []
for i in range(t):
x = int(input())
n.append(x)
for i in range(t):
a = n[i]
fact = 1
while a>0:
fact = fact * a
a = a - 1
print(fact)