To find the sum of individual digits of the given number. (Qbasic Code)

To find the sum of individual digits of the given number. (Qbasic Code)
  • CLS
    INPUT "Enter a number"; n
    s = 0
    WHILE n <> 0
        r = n MOD 10
        s = s + r
        n = n \ 10
    WEND
    PRINT "The sum of the digits is"; s
    END

Using Sub Procedure
DECLARE SUB sum (n)
CLS
INPUT "Enter a number:"; n
CALL sum(n)
END

SUB sum (n)
        WHILE n <> 0
                r = n MOD 10
                s = s + r
                n = INT(n / 10)
        WEND
   PRINT s
END SUB



Using Function Procedure
DECLARE FUNCTION sum (n)
CLS
INPUT "Enter a number:"; n
s = sum(n)
PRINT "The sum of the digits is";s
END

FUNCTION sum (n)
        WHILE n <> 0
                r = n MOD 10
                s = s + r
                n = INT(n / 10)
        WEND
        sum = s
END FUNCTION

Share :

Twitter
More Quotes
Back To Top

facebook main

counter

Powered by Blogger.