Factorial of a Number in java

Factorial Of a Number in java

Given a number n. find the factorial of that number n.

Input Format : The first line will contain a number n whose factorial has to be found out.

Output Format : In the output you need to print an ans denoting the factorial of that number

Note :

Make sure its also satifies long range integers

Constraint : 1<=n<=15

Sample Input 1 : 5

Sample Output 1 : 120

Sample Input 2: 7

Sample Output 2: 5040

Solution of Factorial of a number in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		//your code here
      Scanner sc = new Scanner(System.in);
      long n = sc.nextLong();

      long sum=1;
      for(long i=n;i>0;i--)
        {
          sum=sum*i;
        }
      System.out.println(sum);
	}
}

Add a Comment

Your email address will not be published. Required fields are marked *