Finding Factorial Using "For Loop" | Java Programs

Output:

*** Factorial Using For LOOp Only ***

Find Factorial Of: 4
Factorial of 4 is: 24

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

class FactorialUsingForLoop {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("*** Factorial Using For LOOp Only ***" + "\n");
        System.out.print("Find Factorial Of: ");
        int factorialOf = scanner.nextInt();
        long fact = 1;
        for (int i = 1; i <= factorialOf; i++) {
            fact = fact * i;
        }
        System.out.println("Factorial of " + factorialOf + " is: " + fact);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon