How to calculate factorial in Java

1 Answer

0 votes
package javaapplication1;

import java.io.*;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        int n = 5, fact = 1;
        
        try
        {
            for (int i = 1; i <= n; i++)
                 fact *= i;
            System.out.println(fact);
        }
        catch (Exception e)
        {
           System.out.println(e.toString());
        }  
    }
}

/*

run:

120

*/

 


answered May 6, 2015 by avibootz

Related questions

1 answer 250 views
1 answer 288 views
1 answer 428 views
1 answer 191 views
1 answer 260 views
1 answer 195 views
195 views asked Jan 15, 2025 by avibootz
...