Java Program to Find Factorial of a Number using loop - The Coding Shala
Home >> Java Programs >> Factorial Program using loop
Other Posts You May Like
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
- Java Program to Find the Area of Rectangle
- Java Program to Find the Area of Circle
- Java Program to Find the Area of Triangle
- Java Program to Swap Two Numbers
- Java Program to Reverse a String
Comments
Post a Comment