How to make a factorial program in java by taking input from the user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make a factorial program in java by taking input from the user?

I am in 8th standard from icse board and my teacher asked me this question and i was not able to make the program so please tell me how to do that.

30th Jan 2018, 3:27 PM
Aakersh Chauhan
4 Answers
+ 16
int fact=1; for (int a=1;a <=n;a++) { fact*=a; } //here n is number entered & fact is n!
30th Jan 2018, 3:34 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 15
public int fact (int n){ if (n==1) return 1; else n*fact (n-1);} //u forgot n //@Amrit
30th Jan 2018, 3:41 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
One Method is as given by Gaurav. Another is recursive: public int fact(int n){ if(n==1) return(1); else return n*(fact(n-1));/*Thanks Gaurav for correcting a mistake*/ }
30th Jan 2018, 3:36 PM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 2
thanks
30th Jan 2018, 3:38 PM
Aakersh Chauhan