Friday, March 9, 2018

Qbasic Program to Display Common Divisors / Factors of Two Numbers

Qbasic Program to Display Common Divisors / Factors of Two Numbers

DECLARE SUB DISPLAY( )
CLS
INPUT "enter any two numbers"; a, b
CALL DISPLAY (a, b)
END
SUB DISPLAY ( )
WHILE a MOD b <> 0
    r = a MOD b
    a = b
    b = r
WEND
FOR i = 1 TO r
    IF r MOD i = 0 THEN PRINT i,
NEXT i
END SUB

Input : a = 12, b = 24
Output: 6
// all common divisors are 1, 2, 3, 
// 4, 6 and 12

Input : a = 3, b = 17
Output: 1
// all common divisors are 1

Input : a = 20, b = 36
Output: 3
// all common divisors are 1, 2, 4

No comments:

Post a Comment