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 301 views
1 answer 206 views
1 answer 167 views
167 views asked May 6, 2015 by avibootz
1 answer 103 views
1 answer 168 views
2 answers 155 views
2 answers 196 views
...