//wap to calculate factorial of any number without using loop?
public class FactorialInJava {
public static long factorial(int n)
{
long f=1;
if(n==0)
{
return 1;
}
else
{
f=n*factorial(n-1);
}
return f;
}
public static void main(String[] args) {
long fact=FactorialInJava.factorial(5);
System.out.println("Factorial="+fact);
}
}
o/p:-
Factorial=120
public class FactorialInJava {
public static long factorial(int n)
{
long f=1;
if(n==0)
{
return 1;
}
else
{
f=n*factorial(n-1);
}
return f;
}
public static void main(String[] args) {
long fact=FactorialInJava.factorial(5);
System.out.println("Factorial="+fact);
}
}
o/p:-
Factorial=120
No comments:
Post a Comment