C Program
#include <stdio.h>
int main(){
int n=5, fact=1;
for(int i=1;i<=n;i++) fact*=i;
printf("%d",fact);
}
C Output
Input: n = 5 Output: 120
C++ Program
#include <bits/stdc++.h> using namespace std; int main(){ int n=6, fact=1; for(int i=1;i<=n;i++) fact*=i; cout<<fact; }
C++ Output
Input: n = 6 Output: 720
JAVA Program
class Main{ public static void main(String[] args){ int n=4, fact=1; for(int i=1;i<=n;i++) fact*=i; System.out.print(fact); } }
JAVA Output
Input: n = 4 Output: 24
Python Program
n=7 fact=1 for i in range(1,n+1): fact*=i print(fact)
Python Output
Input: n = 7 Output: 5040
Detailed Explanation
Problem Statement
We must calculate the factorial of a number n. The factorial of n is the product of all positive integers between 1 and n.
The formula is:
n! = n × (n-1) × (n-2) ×. × 1
With the special case 0! = 1.
Step-by-Step Example (C Example: n = 5)
We begin with fact = 1. Then we multiply it by each number from 1 to n.
i=1 → fact = 1 × 1 = 1
i=2 → fact = 1 × 2 = 2
i=3 → fact = 2 × 3 = 6
i=4 → fact = 6 × 4 = 24
i=5 → fact = 24 × 5 = 120
We arrive at 120, which is 5!.
Real-Life Analogy
Consider placing books on a shelf. If you have 5 different books, the number of arrangements possible for placing them is precisely 5!. That is because for the first place you have 5 options, for the second one 4 remaining options, then 3, and so on — multiply them all and you have the total arrangements.
Why Iteration Works Here
As opposed to recursion, the iterative solution is simple and does not carry the function call overhead. We just loop over numbers and multiply, so it's O(n) time and O(1) space.
Complexity
Time Complexity: O(n) since we loop from 1 to n.
Space Complexity: O(1) since we are storing only a constant amount of variables.
Common Mistakes
Not remembering that 0! = 1.
Initializing loop on 0 (which will result in product 0).
Using int for extremely large factorials, which may lead to overflow — for huge numbers, use long long in C++ or Java's BigInteger.
Practical Applications
Factorials occur in combinatorics (permutations, combinations), probability theory, and numerous math-based algorithms. They find applications in areas such as statistics, cryptography, and even game theory.
Learning Insights
This exercise introduces loop control, mathematical definitions, and edge case handling. New programmers also get introduced to integer overflow problems and how the performance of recursion and iteration differ.
SEO-Optimized Closing Paragraph
The iterative factorial algorithm is perhaps the simplest programming problem for entry-level learners. It offers a simple means of learning loops, mathematical reasoning, and efficiency. Practicing the calculation of factorials in C, C++, Java, and Python helps programmers learn by example in writing efficient algorithms, preventing overflows, and manipulating combinatorial math. The idea is of particular importance in competitive programming, interview practices, and actual uses of probability and arrangements.
Social Plugin