Monday, November 18, 2024
Google search engine
HomeData Modelling & AIFactorial of a number without using multiplication

Factorial of a number without using multiplication

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 1: 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)

Approach 2: The problem can be solved by dividing with the reciprocal of the next number instead of multiplying it. In algebra, ab also means a / (1/b). We will be using the same concept to find the factorial of a number without using asterisk. Follow the steps below to solve the problem:

  1. Take a variable of integer type (here: n) which would store the value of which we’re finding the factorial.
  2. Initialize a variable (here: p) which would serve to be the factorial of n.
  3. Start iterating from n to 1. In each step, set the value of p to be the same as that of p divided by the reciprocal of iterator (here: i).
  4. Print p.

Below is the implementation of the above approach in java:

C++




// C++ code for the above approach
 
#include <iostream>
using namespace std;
 
int factorial(int n)
{
   
    // Function to find the factorial of (n) without
    // multiplying.
    int p = 1;
    for (int i = n; i >= 1; i--)
    {
       
        // Loop to calculate the factorial of (n).
        p = p / (1.0 / i);
    }
   
    // Returning the factorial of (n) stored in (p).
    return p;
}
 
int main()
{
 
    int n = 5;
    // Printing the factorial of (n).
    cout << factorial(n) << endl;
    return 0;
}
 
// This code is contributed by lokesh.


Java




public class Factorial {
    int factorial(int n)
    { // Function to find the factorial of (n) without
      // multiplying.
        int p = 1;
        for (int i = n; i >= 1;
             i--) { // Loop to calculate the factorial of
                    // (n).
            p = (int)(p / (1.0 / i));
        }
        return p; // Returning the factorial of (n) stored
                  // in (p).
    }
    public static void main(String[] Args)
    {
        Factorial fact
            = new Factorial(); // Creating an instance of
                               // Factorial class.
        int n = 5;
        System.out.println(fact.factorial(
            n)); // Printing the factorial of (n).
    }
}


Python3




# Python3 code for the above approach
def factorial(n):
    # Function to find the factorial of (n) without multiplying.
    p = 1
    for i in range(n, 0, -1):
       
        # Loop to calculate the factorial of (n).
        p = p / (1.0 / i)
 
    # Returning the factorial of (n) stored in (p).
    return p
 
# Driver code
n = 5
 
# Printing the factorial of (n).
print(factorial(n))
 
#  This code is contributed by phasing17.


C#




// C# code for the above approach
using System;
 
public class Factorial {
 
  int factorial(int n)
  {
     
    // Function to find the factorial of (n) without
    // multiplying.
    int p = 1;
    for (int i = n; i >= 1; i--)
    {
       
      // Loop to calculate the factorial of (n).
      p = (int)(p / (1.0 / i));
    }
     
    // Returning the factorial of (n) stored in (p).
    return p;
  }
 
  static public void Main()
  {
 
    Factorial fact
      = new Factorial(); // Creating an instance of
    // Factorial class.
    int n = 5;
    Console.WriteLine(fact.factorial(
      n)); // Printing the factorial of (n).
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




// JavaScript code for the above approach
 
function factorial(n)
{
   
    // Function to find the factorial of (n) without
    // multiplying.
    let p = 1;
    for (let i = n; i >= 1; i--)
    {
       
        // Loop to calculate the factorial of (n).
        p = p / (1.0 / i);
    }
   
    // Returning the factorial of (n) stored in (p).
    return p;
}
 
    let n = 5;
     
    // Printing the factorial of (n).
    console.log(factorial(n))
 
// This code is contributed by poojaagarwal2.


Output

120

Time Complexity: O(N)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments