Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Java Program to Find Factorial of a Number

Factorial.txt
File size: 600 Bytes

File content type: text/plain

Category: Assignment

Course: B.Tech., ISC Board, and ICSE Board

Semester: 5

/**
 *
 * @program To print Factorial of a number
 * Example: 5! = 1*2*3*4*5
 */

import java.io.*;

public class Factorial {

    public static void main(String args[]) throws IOException {

        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println("Enter number");
        int num = Integer.parseInt(obj.readLine());

        long factorial = 1;
        for(int i = 2 ; i <= num ; i++) {
            factorial = factorial*i;
        }

        System.out.println("Factorial of "+ num +" = "+ factorial);

    }
}
Sample Input/Output

Enter number
8
Factorial of 8 = 40320

Added by passhojao

Comments