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=120Input: 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:
- Initialize a variable ans to N.
- 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.
- 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 operatorint 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 codeint main(){ // Input int N = 5; // Function calling cout << factorialWithoutMul(N) << endl; return 0;} |
Java
// Java program for the above approachimport 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 operatordef 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 codeif __name__ == '__main__': # Input N = 5 # Function calling print(factorialWithoutMul(N)) # This code is contributed by SURENDRA_GANGWAR |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{// Function to calculate factorial of the number// without using multiplication operatorstatic 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 codepublic 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 operatorfunction 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)


