Write a java program to find factorial of a number ?

Write a java program to find factorial of a number in java?

Factorial of a number : For a given value n the factorial of a number is product of all natural numbers less than given value n.

Ex : n!= n * (n-1) * (n-2) *(n-3)

Ex: 5!=5*(5-1)*(5-2)*(5-3)*(5-2)*(5-1)

5!=5*4*3*2*1

we can find the factorial of a number in two ways :

1) Using for loop

2) Using Recursion

First we will write program for finding factorial of a number using for loop in java.Lets write the pseudo code using for loop.

1) Intialize a Integer variable fact with 1.

2) Loop till the given value starting from 1 and multiply each value with i till that given value and return that value.

Ex: suppose if we want to find a factorial of a number 3.

a) First loop starting with 1 since 1 is less than or equal to 3.
fact=fact * i (1*1)=1

b) After incrementing i becomes 2 since 2 is less than or equal to 3.
fact=1*2 =2

c) After incrementing i becomes 3 since 3 is less than or equal to 3.
fact=2*3=6

d) After incrementing i becomes 4 since 4 is not less than or equal to 4 condition fails and return the fact value.

Here is the java code to find factorial of a number without recursion :

public class FactorialTest {

    public static void main(String[] args) {
          Scanner scanner=new Scanner(System.in);
          System.out.println(" Enter the number to find factorial");
          int n=scanner.nextInt();
          System.out.println(factorial(n));
       }

    private  static int factorial(int n) {
        int fact=1;
        for(int i=1;i<=n;i++){
            fact=fact*i;
        }
        return fact;
    }
}

Output :
Enter the number to find factorial
5
120

Here is the java code to find factorial of a number with recursion :

Before writing a program for recursion we will know what is recursion.Recursion is nothing but a function that calls itself.

import java.util.Scanner;

public class FactorialTest {

    public static void main(String[] args) {
          //Used to read the values from input stream such as keyboard.

          Scanner scanner=new Scanner(System.in);

          System.out.println(" Enter the number to find factorial");
          //Reads the values given to input stream as int
          int n=scanner.nextInt();
          //Find the factorial of a number only if the entered value is positive
          if(n>0){

          System.out.println(factorial(n));

          }else{
              System.out.println("Please enter positive number");
          }
       }

    private static int factorial(int n) {
        // If entered value is 0 return 1
        if(n==0){
            return 1;
        }
        // If entered value is not 1 and greater than zero call recursive function with the given value and
        else {
            return n* factorial(n-1);
        }
    }
}

Output :
Enter the number to find factorial
6
720

import java.util.Scanner;

public class FactorialTest {

public static void main(String[] args) {
//Used to read the values from input stream such as keyboard.

Scanner scanner=new Scanner(System.in);

System.out.println(” Enter the number to find factorial”);
//Reads the values given to input stream as int
int n=scanner.nextInt();
//Find the factorial of a number only if the entered value is positive
if(n>0){

System.out.println(factorial(n));

}else{
System.out.println(“Please enter positive number”);
}
}

private static int factorial(int n) {
// If entered value is 0 return 1
if(n==0){
return 1;
}
// If entered value is not 1 and greater than zero call recursive function with the given value and
else {
return n* factorial(n-1);
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *