+ 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.
4 odpowiedzi
+ 16
int fact=1;
for (int a=1;a <=n;a++) {
fact*=a; }
//here n is number entered & fact is n!
+ 15
public int fact (int n){
if (n==1) return 1;
else n*fact (n-1);} //u forgot n
//@Amrit
+ 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*/
}
+ 2
thanks