how to find the factorial of a number in java
pkman:
nahi
Answers
Answered by
1
class FactorialExample2{
static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;//It is the number to calculate factorial
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output:
Factorial of 4 is: 24
Answered by
2
enter a no. and find its factorial .
e.g 5! = 1×2×3×4×5 = 120 .
class Fact
{
public void main( int n )
{
int i = 0, f= 1 ;
for ( i=1; i<=n ; i++ )
{
f= f*i ;
}
System.out.println( " The factorial is " + f ) ;
}
}
it's a blue j input method . I did not use scanner as it's a bit lengthy .
e.g 5! = 1×2×3×4×5 = 120 .
class Fact
{
public void main( int n )
{
int i = 0, f= 1 ;
for ( i=1; i<=n ; i++ )
{
f= f*i ;
}
System.out.println( " The factorial is " + f ) ;
}
}
it's a blue j input method . I did not use scanner as it's a bit lengthy .
Similar questions
Math,
6 months ago
English,
6 months ago
Computer Science,
6 months ago
Chemistry,
1 year ago
Political Science,
1 year ago