You are on page 1of 3

Experiment no 6

Aim: - Write a program to calculate factorial of a number


Theory: The factorial operation is encountered in many areas of mathematics,
notably in combinatorics, algebra, and mathematical analysis. Its most
basic occurrence is the fact that there are n! ways to arrange n distinct
objects into a sequence (i.e., permutations of the set of objects). This
fact was known at least as early as the 12th century, to Indian
scholars. Fabian Stedman, in 1677, described factorials as applied to
change ringing. After describing a recursive approach
In mathematics, the factorial of a non-negative integer n, denoted by
n!, is the product of all positive integers less than or equal to n. For
example,
5! = 5 x 4 x 3 x 2 x 1=120.
The value of 0! is 1, according to the convention for an empty product.
Algorithm: Step
Step
Step
Step

1:
2:
3:
4:

Flowchart:-

Initialize the data segment.


Get the number in AX register
Calculate the factorial of the number.
Display the result.

Program:.model small
.data
a db 05h
.code
start:
mov ax, @data
mov ds, ax
mov al, a
mov cl, a
dec cl
l1:
mul cl
dec cl
jnz l1
mov bx, ax
mov ch, 04h
mov cl, 04h
l2:
rol bx, cl
mov dx, bx
and dx, 0fh
cmp dx, 09h
>9
jbe next
add dx, 07h
next: add dx,30h
Digits/Alphabet
mov ah, 02h
interrupt
int 21h
dec ch
jnz l2
mov ah, 4ch
int 21h
end start
end

;Move data address into ax register


;Move data address ino data segment register
;Move the data into al register
;Move the data into counter cl register
;Decrement counter cl register
;Multiple cl register with data
;Decrement counter cl register
;Jump if counter is not zero at label l1
;Move ax register into bx register
;Number of digits to print
;Number of rotations to perform
;Rotate bx by cl times
;Move data into dx register
;Logical AND with the data
;Compare the number to know if number
;Jump if below or equal to next label
;Add 7 if number >=9
;Add 30 to the number to get ASCII code of
;Move 02h to ah register to print on
;Interrupt to Print
;Decrement count
;Jump if ch is not zero to label l2
;Terminate Program

Output:-

Conclusion: - Hence we have studied and implement Factorial


of a number.

You might also like