Sunday, September 26, 2010

Program for Factorial using Recursion

import java.io.DataInputStream;
class Fact
{
int fac(int n)
{
if(n==1)
{
return(1);
} 
else
{
return(n*fac(n-1));
}
}
public static void main(String args[])throws Exception
{
Fact b=new Fact();
int f,n;
DataInputStream o=new DataInputStream(System.in);
System.out.println("Enter the value:");
n=Integer.parseInt(o.readLine());
f= b.fac(n);
System.out.println(" ");
System.out.println("Factorial of "+n+" = "+f);
}
}

/*
Enter the value:
5

Factorial of 5 = 120

Process completed.

*/

No comments:

Post a Comment