Factorial! | Competitive Programming Questions with Answers in Java

Competitive Programming Questions with Answers in Java

Factorial!

Problem

You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N.

Input Format
The first and only line of the input contains a single integer N denoting the number whose factorial you need to find.

Output Format
Output a single line denoting the factorial of the number N.

Constraints
1≤N≤1

Sample Input
2

Sample Output
2

Solution

import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
       
        int i, n, fact=1;
        Scanner s = new Scanner(System.in);
        n = Integer.parseInt(s.nextLine());
        for (i=2; i<=n; i++)
        {
            fact= fact * i;
        }
        System.out.println(fact);
    }
}

Comments