Factorial | Practice | GeeksforGeeks

Given a positive integer, N. Find the factorial of N.
 
Example 1:
Input:
N = 5
Output:
120
Explanation:
5*4*3*2*1 = 120
Example 2:
Input:
N = 4
Output:
24
Explanation:
4*3*2*1 = 24


Your Task:
You don't need to read input or print anything. Your task is to complete the function factorial() which takes an integer N as input parameters and returns an integer, the factorial of N.
 
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
0 <= N <= 18
0
mayank180919991 week ago
long long int factorial(int N){
       //code here
       long long int fact=1;
       for(int i=1;i<=N;i++){
           fact=fact*i;
       }
   return fact;
}
0
purnimasharma13051 month ago
public:   long long int factorial(int N){       if(N == 0)           return 1;       return N*factorial(N-1);          }
0
abhishekkr50082 months ago
 //In 0.1 Try with StreamsAPI
static long factorial(int N){        java.util.stream.LongStream list=java.util.stream.LongStream.range(1,N+1);        Long sum = list.reduce(1, (a, b) -> a*b);        return sum;   }
0
aakasshuit2 months ago
//Java Solution

        long fact=1;
        for(int i=N;i>0;i--){
            fact*=i;
        }
        return fact;
+1
mrunmai51152 months ago
long long int factorial(int N){
       //code here
       unsigned long long int product = 1;
       for(int i = 1; i <= N; i++){
           product *= i;
       }
       return product;
}
0
manishamali2 months ago
CPP Solution Total Time Taken 0.0
long long int factorial(int N){      long long fact=1;      while(N>0){          fact*=N;          N=N-1;      }      return fact;} 
0
barotlove202 months ago
long long int factorial(int N){
       long long int ans = 1;
       for(int i=N;i>0;--i)
       {
           ans *= i;
       }
       return ans;
}

0
nishabhatt093362 months ago
C(gcc 5.4)
long long int factorial(int N){       long long f=1,i,facto;for(i=N;i>=1;i--)   {f=f*i;} facto=f;return facto;}
+1
darshitagupta2 months ago
simple c++ soln
long long int factorial(int N){      //code here      if(N==1 || N==0)return 1;      else      return N*factorial(N-1);}
 
0
alokaryan513 months ago
class Solution{   static long factorial(int N){       // code here       if (N == 0 || N ==1){           return 1;       }       long ans = 1;              for(int i = 1; i <= N; i++){           ans = ans * i;       }       return ans;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.