Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 7, 2020

Best Way To Find the Factorial of a Number In java (Iterative and Recursive)

1. Overview


In this programming series, Today we are going to learn how to find the factorial for a given number using iterative and recursive approach.


In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n: The following is the formulae to find the factorial.

n! =  n * (n-1) * (n-2) * (n-3) * ..... * 3 * 2 * 1

For example,

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

An iterative approach is running a loop for n times and executing the same set of code using for loop or while loop whereas in recursive approach is calling the same function from itself for n times.
Java Program to Find the Factorial of a Number using Iterative and Recursive




2. Iterative Java Program to Find Factorial using for loop


In the Iterative approach, Running a for loop for n times where n is the given number.

Example:

package com.java.w3schools.blog.java.program.to;

import java.util.Scanner;

/**
 * 
 * Iterative java program to find the factorial using for loop
 * 
 * @author JavaProgramTo.com
 *
 */
public class FactorialExample {

 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int n = scanner.nextInt();

  int factorialResult = 1;

  // using for loop
  for (int i = 1; i <= n; i++) {

   factorialResult = factorialResult * i;
  }

  System.out.println("Factorial value for " + n + " is : " + factorialResult);

 }

}

Output:

Enter a number : 
5
Factorial value for 5 is : 120

3. Iterative Java Program to Find Factorial using while loop


The above program is implemented using for loop. Now, we will see the same using a while loop. Both fall under the iterative approach.

Example:


package com.java.w3schools.blog.java.program.to;

import java.util.Scanner;

/**
 * 
 * Iterative java program to find the factorial using while loop
 * 
 * @author JavaProgramTo.com
 *
 */
public class FactorialWhileLoopExample {

 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int n = scanner.nextInt();

  int factorialResult = 1;

  // using while loop
  int i = 1;
  while (i <= n) {

   factorialResult = factorialResult * i;
   i++;
  }

  System.out.println("Factorial value for " + n + " using while loop is : " + factorialResult);

 }

}

Output:


Enter a number : 
5
Factorial value for 5 using while loop is : 120

4. A recursive approach to find factorial

The below program is completely done using the recursive method and also an interview question.

package com.java.w3schools.blog.java.program.to;

import java.util.Scanner;

/**
 * 
 * Iterative java program to find the factorial using Recursive concept.
 * 
 * @author JavaProgramTo.com
 *
 */
public class FactorialRecursiveExample {

 public static void main(String[] args) {

  System.out.println("Enter a number : ");
  Scanner scanner = new Scanner(System.in);
  int n = scanner.nextInt();

  int factorialResult = factorial(n);

  System.out.println("Factorial value for " + n + " using recursive logic is : " + factorialResult);

 }

 /**
  *
  * Recursive function for factorial program.
  * 
  * @param n
  * @return
  */
 private static int factorial(int n) {

  if (n == 1)
   return 1;

  return n * factorial(n - 1);
 }

}

This program output will be the same as iterative approaches.

5. Conclusion


In this article, We've seen what is the factorial term in mathematics and what is the formulae. Seen the example programs using iterative and recursive approach.



No comments:

Post a Comment

Please do not add any spam links in the comments section.