How to calculate factorial of a number in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int factorial = 1;  
        int number = 6;
        
        for (int i = 1; i <= number; i++){    
            factorial = factorial * i;    
        }    
        
        System.out.println("Factorial of " + number + " is: " + factorial);    
    }
}




/*
run:

Factorial of 6 is: 720

*/

 



answered Apr 8, 2021 by avibootz

Related questions

1 answer 294 views
1 answer 196 views
1 answer 161 views
161 views asked May 6, 2015 by avibootz
1 answer 94 views
1 answer 155 views
2 answers 147 views
2 answers 188 views
...