The Programming Project: factorial
Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Sunday, September 29, 2013

PROGRAMMING IN 8086

Fact = N * (N–1)*….2*1  (all are unsigned Word variable)


 TITLE factorial 
;TO COMPUTE
;factorial N <= 8
;----------------------------------------------------------------
     .MODEL SMALL
     .STACK 64
     .DATA
     VAR_N DW 9 
     VAR_FACT DW 1
;----------------------------------------------------------------
     .CODE
MAIN PROC FAR
     MOV AX,@DATA
     MOV DS,AX
     MOV AX,VAR_N 
     MOV DX,01
     CMP AX,00h
     JE SPECIAL
     MOV CX,AX
     MOV BX,AX
     DEC BX      
     DEC CX  
     JZ SPECIAL
     LOOP: MUL BX
           DEC BX 
           DEC CX
           JNZ LOOP         
     MOV DX,AX
     SPECIAL:MOV VAR_FACT,DX
     MOV AX,4C00H
     INT 21H
MAIN ENDP
     END MAIN
    

Saturday, September 14, 2013

Calculating Factorial in Python

A simple program in Python to calculate the factorial of an natural number:

#File: fact.py
#A simple program
def main():
    print "Factorial Function"
    x=input("Enter a natural number:")
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
main()

Run the python interpreter from the folder where the file (fact.py) is residing and import it as shown below:


administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fact
Factorial Function
Enter a natural number:350
123587405826548875014395199766546457224532073946919515879429330230093035357491314216934583295011178445941552109432761532449767761892237043444942213964090091669490545661255111334533069825455607852789836451585122902099649977304226794874840601811017764137584868137504975397325925882541777117706619490238363409254589994079334626893194608016888986949684994333459029365214555784862353939102567266745712846824819004146064184543888123533464975621179287075018586481357643313075153359002713294611632614208134036650116689052585573350955360246170451786972351365370405722036294385680478287278827977511411909071460914807681131728232182991517416470483157998067487290163200000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>>


Wednesday, May 1, 2013

Factorial in 8085 Programming


To find the factorial of n <=6 stored at 0000H. Result stored at memory location 0001H

LDA 0000H
MOV B,A
CPI 00H
JZ LAST1
CPI 01H
JZ LAST1
XRA A
MOV D,B
DCR B
MOV C,B
MOV E,C
LOOP: ADD  D
DCR C
JNZ LOOP
MOV D,A
XRA A
DCR E
MOV C,E
DCR B
JNZ LOOP
MOV A,D
STA 0001H
JMP LAST
LAST1: MVI A,01H
STA 0001H
LAST: HLT