In this post i will explain how to write a program to get the factorial of a given number.I have written two programs.First one uses for loop to find the factorial and second one uses recursion.But before that i will explain how to find the factorial.
Suppose number is 4.Then to find out factorial of any given number we will multiply it with (number-1) until number becomes 1.Since the factorial of 1 is 1 So we end the procedure there.
factorial(4)=4*3*2*1
=24
factorial(5)=5*4*3*2*1
=120
So in this way we can find out the factorial of any given number.
So in the following program, I am taking the input from user in main method and then called the getFactorial() function which takes the number as argument.In method getFactorial(), I have taken a local variable res which will hold the result.Then i started a for loop in i am multiplying the number with res and after each iteration the value of i is decremented by 1.
for 1st iteration,
i=4 res=1
res=1* 4
=4
for 2nd iteration,
i=3 res=4
res= 4*3
=12
for 3rd iteration,
i=2 res=12
res=12*2
=24
for 4th iteration,
i=1 res=4
res=24*1
=24
for 5th iteration,
i=0 res=24
it will exit from for loop
The following program is a recursion based program.
res=res*getFactorial(number-1);
Suppose number=4
Then
res=4*getFactorial(3)
then method will be called again until getFactorial(1) where first condition will be satisfied it will be returned to its previous call and factorial will be calculated.
res=4*3*2*getFactorial(1)
=4*3*2*1
=24
So in this way we can calculate the Factorial of any given number.
Suppose number is 4.Then to find out factorial of any given number we will multiply it with (number-1) until number becomes 1.Since the factorial of 1 is 1 So we end the procedure there.
factorial(4)=4*3*2*1
=24
factorial(5)=5*4*3*2*1
=120
So in this way we can find out the factorial of any given number.
So in the following program, I am taking the input from user in main method and then called the getFactorial() function which takes the number as argument.In method getFactorial(), I have taken a local variable res which will hold the result.Then i started a for loop in i am multiplying the number with res and after each iteration the value of i is decremented by 1.
Program Code:
package Progs;
import java.util.Scanner;
public class Factorial {
private static int getFactorial(int number){
int res=1;
for(int i=number;i>=1;i--){
res=res*i;
}
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to calculate its factorial");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
int result=getFactorial(number);
System.out.println("Factorial of given number is "+result);
}
}
Output:
Enter the number to calculate its factorial
4
Factorial of given number is 24
Suppose number=4 package Progs;
import java.util.Scanner;
public class Factorial {
private static int getFactorial(int number){
int res=1;
for(int i=number;i>=1;i--){
res=res*i;
}
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to calculate its factorial");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
int result=getFactorial(number);
System.out.println("Factorial of given number is "+result);
}
}
Output:
Enter the number to calculate its factorial
4
Factorial of given number is 24
for 1st iteration,
i=4 res=1
res=1* 4
=4
for 2nd iteration,
i=3 res=4
res= 4*3
=12
for 3rd iteration,
i=2 res=12
res=12*2
=24
for 4th iteration,
i=1 res=4
res=24*1
=24
for 5th iteration,
i=0 res=24
it will exit from for loop
The following program is a recursion based program.
Program Code:
package Progs;
import java.util.Scanner;
public class Factorial_Rcursion {
private static int getFactorial(int number) {
int res=number;
if(number==0)
return 1;
res=res*getFactorial(number-1);
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to calculate its factorial");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
int result=getFactorial(number);
System.out.println("Factorial of given number is "+result);
}
}
Output:
Enter the number to calculate its factorial
5
Factorial of given number is 120
In getFactorial() method again i have taken a local variable which will hold the result.Then i have checked for exit condition i.e.if number==1 and after that i have written a expression which will calculate the factorial of number recursivelypackage Progs;
import java.util.Scanner;
public class Factorial_Rcursion {
private static int getFactorial(int number) {
int res=number;
if(number==0)
return 1;
res=res*getFactorial(number-1);
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to calculate its factorial");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
int result=getFactorial(number);
System.out.println("Factorial of given number is "+result);
}
}
Output:
Enter the number to calculate its factorial
5
Factorial of given number is 120
res=res*getFactorial(number-1);
Suppose number=4
Then
res=4*getFactorial(3)
then method will be called again until getFactorial(1) where first condition will be satisfied it will be returned to its previous call and factorial will be calculated.
res=4*3*2*getFactorial(1)
=4*3*2*1
=24
So in this way we can calculate the Factorial of any given number.
0 comments:
Post a Comment