Theory:--
The factorial of a number n, denoted as n!, is the product of all positive integers from 1 to n.
It is defined only for non-negative integers, with 0! = 1 as a special case.
Factorials grow very rapidly, leading to large values even for small inputs.
They are widely used in permutations, combinations, probability, and various mathematical computations.
Factorial operations are typically implemented using loops or recursion in programming and digital logic.
Verilog Code (Behavioural Description):--
module fact_num(
input wire [2:0] n,
output reg [15:0] fact
);
integer i;
always @(*) begin
fact = 1;
for (i = 1; i <= n; i = i + 1)
fact = fact * i;
end
endmodule
Test Bench:--
module tb_fact_num;
reg [2:0] n;
wire [15:0] fact;
fact_num DUT (n, fact);
initial begin
n = 0; #10;
n = 1; #10;
n = 2; #10;
n = 3; #10;
n = 4; #10;
n = 5; #10;
n = 6; #10;
$finish;
end
endmodule
OUTPUT WAVEFORM:--
pin assignment:
i/p=j15,l16,m13,r15
o/p=h17,k15,j13,n14,r18,u17,v17,u16,v16,t15,u14,t16,v15,v14,v12,v11