# Factorial of a number without using multiplication

• Difficulty Level : Medium
• Last Updated : 02 Jul, 2021

Given a positive number N, the task is to calculate the factorial of N without using the multiplication operator.

Examples:

Input: N = 5
Output:
120
Explanation:
5*4*3*2*1=120

Input: N = 7
Output:
5040

Observation:

```A*B=A+A+A+A...B times.

This observation can be used as follows:
5!=5*4*3*2*1
=(5+5+5+5)*3*2*1
=(20+20+20)*2*1
=60+60
=120```

Approach: The problem can be solved using the concept of nested loops. Instead of using the multiplication operator, the answer can be manually calculated by using another loop. Follow the steps below to solve the problem:

1. Initialize a variable ans to N.
2. Iterate from N-1 to 1, using the variable i, and do the following:
• Initialize a variable sum to 0.
• Iterate from 0 to i-1, using the variable j, and add ans to sum
3. Print ans.

Below is the implementation of the above approach

## C++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to calculate factorial of the number` `// without using multiplication operator` `int` `factorialWithoutMul(``int` `N)` `{` `    ``// variable to store the final factorial` `    ``int` `ans = N;`   `    ``// Outer loop` `    ``for` `(``int` `i = N - 1; i > 0; i--) {` `        ``int` `sum = 0;`   `        ``// Inner loop` `        ``for` `(``int` `j = 0; j < i; j++)` `            ``sum += ans;` `        ``ans = sum;` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``// Input` `    ``int` `N = 5;`   `    ``// Function calling` `    ``cout << factorialWithoutMul(N) << endl;` `    ``return` `0;` `}`

## Java

 `// Java program for the above approach`   `import` `java.io.*;`   `class` `GFG {` `    ``// Function to calculate factorial of the number` `    ``// without using multiplication operator` `    ``public` `static` `int` `factorialWithoutMul(``int` `N)` `    ``{` `        ``// variable to store the final factorial` `        ``int` `ans = N;`   `        ``// Outer loop` `        ``for` `(``int` `i = N - ``1``; i > ``0``; i--) {` `            ``int` `sum = ``0``;`   `            ``// Inner loop` `            ``for` `(``int` `j = ``0``; j < i; j++)` `                ``sum += ans;` `            ``ans = sum;` `        ``}` `        ``return` `ans;` `    ``}` `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``5``;`   `        ``// Function calling` `        ``System.out.println(factorialWithoutMul(N));` `        ``// This code is contributed by Potta Lokesh` `    ``}` `}`

## Python3

 `# Python3 program for the above approach`   `# Function to calculate factorial of the number` `# without using multiplication operator` `def` `factorialWithoutMul(N):` `    `  `    ``# Variable to store the final factorial` `    ``ans ``=` `N`   `    ``# Outer loop` `    ``i ``=` `N ``-` `1` `    `  `    ``while` `(i > ``0``):` `        ``sum` `=` `0`   `        ``# Inner loop` `        ``for` `j ``in` `range``(i):` `            ``sum` `+``=` `ans` `            `  `        ``ans ``=` `sum` `        ``i ``-``=` `1` `        `  `    ``return` `ans`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Input` `    ``N ``=` `5`   `    ``# Function calling` `    ``print``(factorialWithoutMul(N))` `    `  `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{`   `// Function to calculate factorial of the number` `// without using multiplication operator` `static` `int` `factorialWithoutMul(``int` `N)` `{` `    `  `    ``// Variable to store the final factorial` `    ``int` `ans = N;`   `    ``// Outer loop` `    ``for``(``int` `i = N - 1; i > 0; i--)` `    ``{` `        ``int` `sum = 0;`   `        ``// Inner loop` `        ``for``(``int` `j = 0; j < i; j++)` `            ``sum += ans;` `            `  `        ``ans = sum;` `    ``}` `    ``return` `ans;` `}`   `// Driver code` `public` `static` `void` `Main()` `{` `    `  `    ``// Input` `    ``int` `N = 5;`   `    ``// Function calling` `    ``Console.Write(factorialWithoutMul(N));` `}` `}` `        `  `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 `   ```

Output

`120`

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :