Skip to content
Related Articles

Related Articles

Improve Article

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
    • Add sum to ans.
  3. Print ans.

Below is the implementation of the above approach

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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




   <script>
// JavaScript program for the above approach
 
// Function to calculate factorial of the number
// without using multiplication operator
function factorialWithoutMul(N)
{
    // variable to store the final factorial
    let ans = N;
 
    // Outer loop
    for (let i = N - 1; i > 0; i--) {
        let sum = 0;
 
        // Inner loop
        for (let j = 0; j < i; j++)
            sum += ans;
        ans = sum;
    }
    return ans;
}
 
// Driver code
 
    // Input
    let N = 5;
 
    // Function calling
    document.write(factorialWithoutMul(N));
 
  // This code is contributed by Potta Lokesh
    </script>
Output
120

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

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :