Factorial of a Number: flowchart, algorithm and program

This program reads a number n and prints n! (n factorial), which is the product 1 × 2 × 3 × ... × n. It starts with fact = 1 and multiplies it by every number from 1 up to n inside a for loop.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: n, i; fact = 1.
  3. Step 3: Read n.
  4. Step 4: Set i = 1.
  5. Step 5: If i <= n, go to step 6; otherwise go to step 8.
  6. Step 6: Set fact = fact * i.
  7. Step 7: Set i = i + 1. Go to step 5.
  8. Step 8: Print fact.
  9. Step 9: Stop.

Pseudocode

BEGIN
    n, i; fact ← 1
    READ n
    i ← 1
    WHILE i <= n DO
        fact ← fact * i
        i ← i + 1
    END WHILE
    PRINT fact
END

Program in C, Python and Java

Factorial of a Number program in C

#include <stdio.h>

int main() {
    int n, i;
    long long fact = 1;

    printf("Enter n: ");
    scanf("%d", &n);

    for (i = 1; i <= n; i++) {
        fact = fact * i;
    }

    printf("Factorial of %d = %lld\n", n, fact);
    return 0;
}

Factorial of a Number program in Python

n = int(input("Enter n: "))
fact = 1

for i in range(1, n + 1):
    fact = fact * i

print("Factorial of", n, "=", fact)

Factorial of a Number program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, i;
        long fact = 1;

        System.out.print("Enter n: ");
        n = sc.nextInt();

        for (i = 1; i <= n; i++) {
            fact = fact * i;
        }

        System.out.println("Factorial of " + n + " = " + fact);
    }
}

Sample input: n = 5
Output: Factorial of 5 = 120

How it works

  • fact is a long long and starts at 1, because 1 is the neutral value for multiplication. Starting at 0 would make every result 0.
  • The loop runs with i from 1 to n (i <= n), so each whole number in that range is visited exactly once.
  • In every pass, fact = fact * i multiplies the running product by the current i. For n = 5, fact goes 1, 2, 6, 24, 120.
  • When i becomes n + 1 the condition fails, the loop stops, and fact holds n!.
  • The result is printed with %lld because fact is a long long. A plain int would overflow for n above 12, while long long is safe up to n = 20.

Dry run (trace table)

StepLineBoxfactniOutput
13Start
25n, i; fact = 11
38Read n15Enter n:
410i = 1151
510i <= n?151
611fact = fact * i151
710i = i + 1152
810i <= n?152
911fact = fact * i252
1010i = i + 1253
1110i <= n?253
1211fact = fact * i653
1310i = i + 1654
1410i <= n?654
1511fact = fact * i2454
1610i = i + 12455
1710i <= n?2455
1811fact = fact * i12055
1910i = i + 112056
2010i <= n?12056

Time and space complexity

Time: O(n) · Space: O(1). The loop runs n times doing constant work each time, using only a few variables.

Common mistakes

  • Initialising fact to 0 instead of 1, which makes the answer always 0.
  • Using int for fact and printing with %d. The value overflows quickly (13! is already too big for a 32-bit int), and a wrong format specifier like %d for a long long gives garbage output.
  • Starting the loop at i = 0 or stopping at i < n. Starting at 0 zeroes the product, and i < n leaves out n itself.

Viva questions

Q1. Why is fact initialised to 1 and not 0?

Because fact is multiplied in every iteration. If it started at 0, every product would stay 0. 1 is the multiplicative identity, so it does not change the result.

Q2. What does this program print for n = 0, and why?

It prints 1. The loop condition i <= n is false at the start (1 <= 0), so the loop never runs and fact stays at its initial value 1. That matches the definition 0! = 1.

Q3. What happens if the user enters a negative number such as -5?

The loop does not run because 1 <= -5 is false, so it prints 'Factorial of -5 = 1'. This is wrong, since factorial is not defined for negatives. The program should check n < 0 and print an error message.

Q4. Why is fact declared as long long instead of int?

Factorials grow very fast. A 32-bit int overflows at 13!, while long long (64-bit) holds values up to 20!. The %lld format specifier is used to print it.

Q5. What happens if n = 21 or larger?

21! is larger than the maximum long long value, so the multiplication overflows and the output is garbage. To handle bigger n you would need unsigned long long (up to 20!), a double, or a big-integer array.

Q6. Why did you use a for loop here, and where does the loop start and end?

The number of repetitions is known in advance (n times), so a for loop fits. It starts at i = 1, because multiplying by 0 would zero the result, and goes up to i = n inclusive.

Q7. What is the time complexity of this program, and what about space?

Time is O(n), since there is one multiplication per iteration and n iterations. Space is O(1), because only a few variables are used.

Q8. What would change if you wrote this using recursion?

You would write fact(n) = n * fact(n-1) with the base case fact(0) = 1. Time stays O(n), but the space becomes O(n) because of the call stack, and a very large n could cause a stack overflow.

Frequently asked questions

What does the program print for n = 0?

It prints 1. The loop body never runs because 1 <= 0 is false, so fact stays at its starting value of 1, which matches the definition 0! = 1.

Why is fact declared as long long instead of int?

Factorials grow very fast. An int overflows after 12!, while a long long can hold values up to 20!, so it handles a much larger range of inputs.

What happens if the user enters a negative number?

The loop condition i <= n is false straight away, so the program prints 1, which is wrong because factorial is not defined for negative numbers. Add an if (n < 0) check to print an error message.

More programs

← All programs

Try AI Flowchart free →