small factorial problem

problem name is FLOW018.
it is under practice>begineer>FLOW018
my submission gives wrong answer.but why?

#include<stdio.h>
int fact(int);
int main()
{
int n,t,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
scanf("%d",&n);
printf(“factorical is%d”,fact(n));
return 0;
}
}
int fact(n)
{
int p,i;
p=1;
for(i=n;i>0;i–){
p=p*i;
}
return p;
}

There are multiple problems with your code. I recommend you to fix one problem at a time and run the code so you really understand what was wrong with it.

  1. The ‘return 0;’ should be outside the for loop because otherwise your program would just run for 1 test case then terminate.
  2. Don’t print the answer as “factorial is%d” because the output format doesn’t specify anything like that. Instead, just print the number without any words.
  3. Put a ‘\n’ after printing the answer. Even if you were getting the correct answer the numbers would be concatenated together. For example the answer for the sample test case would be printed as 624120.
  4. In this problem, n can be as large as 20 and 20! is around 2\cdot 10^{18} but the max value you can store in the int datatype is around 2 \cdot 10^9 so it will overflow and give a wrong answer.

Also don’t write your code in the question. Just post a link to your submission which you can find by clicking on “My submissions” on the problem page.

And lastly, don’t get discouraged at any point; Instead, learn from your mistakes. Have fun coding!

1 Like

again,there is problem

I’ve submitted the below code but it didn’t get accepted. Can anyone please help me out?

import java.util.*;

class Main{
public static void main(String args[]){

Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int fact=1;	
for(int i=0;i<t;i++){
	int n=sc.nextInt();
	for(int j=1;j<=n;j++){
	     fact=fact*j;
	}
	System.out.println(fact);
	fact=1;	
}
}

}