Java Program to Find Factorial of a Number using loop - The Coding Shala

Home >> Java Programs >> Factorial Program using loop

In this post, we will learn how to find Factorial of a Number using a loop in Java.

Java Program to Find Factorial of a Number using Loop

Factorial of a non-negative number is the multiplication of numbers from 1 to n [n including]. Factorial of n is represented by n!.

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

Example 1:
Input: 5
Output: 120
Explanation: 1 * 2 * 3 * 4 * 5 = 120

Java Program:

// Java program to Find Factorial using loop

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// take number input
System.out.println("Enter the number");
long num = sc.nextLong();

long fact = 1;

// find multiplication of 1 to n numbers using loop
for(int i = 1; i <= num; i++) {
fact = fact * i;
}

System.out.println("Factorial of " + num + " is: " + fact);

sc.close();
}
}

Output:

Enter the number
10
Factorial of 10 is: 3628800

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.