Understanding Computer Programming

Osher Lifelong Learning Institute
University of Illinois, Urbana-Champaign

Scott Badman, Instructor


Topic: Prime program implemented in QuickBasic fully modularized

February 11, 2016


PRINT "Please enter a number to see if it is a prime:"
INPUT number

IF number < 0 OR number > 1000000 THEN

    PRINT "Number out of range."

ELSE

    prime$ = determineIfPrime(number)

    IF prime$ = "True" THEN
        PRINT number; " is a prime."
    ELSE
        PRINT number; " is not a prime."
    END IF

END IF


FUNCTION determineIfPrime$(number)

    prime$ = "True"

    IF number = 0 OR number = 1 THEN

        prime$ = "False"

    ELSE

        divisor = 2

        WHILE divisor < number

            remainder = modulo(number, divisor)

            IF remainder = 0 THEN
                prime$ = "False"
            END IF

            divisor = divisor + 1

        WEND

        determineIfPrime = prime$

    END IF

END FUNCTION


FUNCTION modulo(number, divisor)

    intermediate = number

    WHILE intermediate >= divisor
        intermediate = intermediate - divisor
    WEND

    remainder = intermediate

    modulo = remainder

END FUNCTION


Understanding Computer Programming

Osher Lifelong Learning Institute
University of Illinois, Urbana-Champaign

Scott Badman, Instructor