Pages

Tuesday, July 3, 2012

find the Factorial of a number using Recursion

Write a program to find the Factorial of a number using Recursion. Factorial can be defined as Factorial(n) = 1 * 2 * 3 ….* (n-1) * n.

class rfactorial
{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
int fact;

recursion r1=new recursion();
fact=r1.rec(num);
System.out.println("The factorial for "+num+" is = "+fact);
}
}

class recursion
{
public int rec(int a)
{
int f;
if(a==1 || a==0)
{
return(1);
}
else
{
f=a*rec(a-1);
return(f);
}
}
}

No comments:

Post a Comment