Problem 3 • Math
Author: Er. Manoj Kumar • Computer Science Fundamentals
Calculate Factorial of an Integer (N!)
📋
Problem Statement
Given a non-negative integer N, compute its factorial N! = 1 * 2 * 3 * ... * N, with appropriate validation for 0! = 1 and error handling for negative inputs.
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| N | Integer | Input number (0 <= N <= 20) |
| fact | Unsigned Long Long | Product accumulator, initialized to 1 |
| i | Integer | Loop counter running from 1 to N |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read integer N.
Step 3: If N < 0:
Print 'Error: Factorial undefined for negative numbers' and Stop.
Step 4: Initialize fact = 1, i = 1.
Step 5: While (i <= N):
fact = fact * i
i = i + 1
Step 6: Print N, '! = ', fact.
Step 7: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/"Read N"/]
In --> Valid{"Is N < 0 ?"}
Valid -- Yes --> Err[/"Print 'Factorial Undefined for Negative'"/]
Err --> Stop([Stop])
Valid -- No --> Init["fact = 1, i = 1"]
Init --> Cond{"Is i <= N ?"}
Cond -- Yes --> Calc["fact = fact * i
i = i + 1"] Calc --> Cond Cond -- No --> Out[/"Print fact"/] Out --> Stop
i = i + 1"] Calc --> Cond Cond -- No --> Out[/"Print fact"/] Out --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
/ \
/ N < 0? \ ---YES--> [Print Undefined] --> [STOP]
\ /
| NO
v
+-----------------+
| fact = 1, i = 1 |
+-----------------+
|
v
/ \
/ i <= N? \ ---NO---> [Print fact] ------> [STOP]
\ /
| YES
v
+-----------------+
| fact = fact * i |
| i = i + 1 |
+-----------------+
|
+------> (Loop back to i <= N?)
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| N=0 | fact=1, i=1 | Loop cond 1 <= 0 is False | Loop skipped immediately | 0! = 1 |
| N=5, Step 1-5 | i=1..5 | fact: 1->2->6->24->120 | 1*2*3*4*5 | 5! = 120 |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_3.cpp
GCC 13.2 • C++20
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a non-negative integer (0 to 20): ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter an integer.\n";
return 1;
}
if (n < 0) {
cout << "Error: Factorial is undefined for negative integers!\n";
return 1;
}
unsigned long long fact = 1;
for (int i = 1; i <= n; ++i) {
fact *= i;
}
cout << n << "! = " << fact << "\n";
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 6 Output: 6! = 720
Developer & Exam Key Takeaway
Factorial grows super-exponentially. In standard C++, unsigned long long tops out at 20! (2,432,902,008,176,640,000). Beyond N=20, big integer libraries or arrays are mandatory.