Wednesday, 28 August 2013

Recursive Function In Java


Definition Of Recursive Function: A Function or Method that calls itself again and again is called a Recursive Function. This technique can be used in a controlled way to achieve a specific output without use of loops.


 Here a Program To Find Factorial Of An Integer Through Recursive Function


//Program Starts Here 
import java.util.Scanner;
class fact
{
long num;
long f(long i)
{
num=i;
if(num==0) return 1;
else return(num*f(num-1)); //Recursion of function used here
}
}

class find
{
public static void main(String arg[])
{
fact A=new fact();
long n;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter an integer to find its factorial : ");
n=sc.nextInt();
System.out.print("\nFactorial Of "+n+" is : "+A.f(n)+"\n");
}
}
//Program Ends Here 

For More Java Programs Click Here

Thanks For Visiting... 

No comments:

Post a Comment