write a java program do-find factorial
given number (static method)
Answers
Answer:
Factorial Program using loop in java
class FactorialExample{
1.public static void main(String args[]){
int i,fact=1;
2.int number=5;//It is the number to calculate factorial.
3.for(i=1;i<=number;i++){
4.fact=fact*i;
5.}
6.System.out.println("Factorial of "+number+" is: "+fact);
Factorial Program using loop in java:
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Factorial Program using recursion in java:
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);
}
}
Follow me please.