0% found this document useful (0 votes)
12 views2 pages

Factorial

Uploaded by

cadillacvua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Factorial

Uploaded by

cadillacvua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical-1.

2: Program to find factorial of a number Using Recursive and Iterative methods:

public class factorial


{
static int rec_factorial(int n)
{
if(n==0||n==1)
{
return 1;
}
else
{
return(n*factorial(n-1));
}
}
int ite_factorial(int n)
{
int fact=1;
if(n==0||n==1)
{
return 1;
}
else
{
for (int i=n;i>1;i-- )
{
fact=fact*i;
}
return fact;
}
}
public static void main(String[] args)
{
int num1 = 5;
int num2=4;
System.out.println("Using recursive method");
System.out.println("Factorial of "+ num1 + " is " + rec_factorial(num1));
System.out.println("Using iterative method");
System.out.println("Factorial of "+ num2 + " is " + ite_factorial(num2));
}
}
Output:

Using recursive method


Factorial of 5 is 120
Using iterative method
Factorial of 4 is 24

You might also like