Java program to find factorial of a number using for loop
ADVERTISEMENTS
Java program to find factorial of a number using for loop, there are you will learn how to make a Java program to find the factorial of any integer numbers.
Before making this program you have to knowledge of factorial formula to complete out this.
Formula to find the factorial value of any number
factorial of x (n!) = 1 * 2 * 3 * 4....n
Let us understand this through a java program:
// Java program to find the factorial of any number using for loop
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value to find the fact::\n");
int x = in.nextInt(), i, f = 1;
// x - denotes input number
// f - denotes factorial value
if (x > 0) {
for (i = 1; i <= x; ++i) {
f *= i;
}
System.out.println("Factorial of " + x + " = " + f + "\n");
} else {
System.out.println("Sorry, input number should be positive number greater than 1!\n");
}
}
}
Output:
Enter an integer value to find the fact::
7
Factorial of 7 = 5040
Tags:
factorial program in java using while loop
factorial program in java using scanner
find the number for given factorial in java
factorial program in java using do-while loop