QBasic Programs
Section 1: Function Programs
Product of three numbers
DECLARE FUNCTION Product(a, b, c)
CLS
INPUT "Enter three numbers: ", x, y, z
PRINT "Product = "; Product(x, y, z)
END
FUNCTION Product(a, b, c)
Product = a * b * c
END FUNCTION
Volume and Surface Area of a Box
DECLARE FUNCTION Volume(l, b, h)
DECLARE FUNCTION Surface(l, b, h)
CLS
INPUT "Enter length, breadth and height: ", l, b, h
PRINT "Volume = "; Volume(l, b, h)
PRINT "Surface Area = "; Surface(l, b, h)
END
FUNCTION Volume(l, b, h)
Volume = l * b * h
END FUNCTION
FUNCTION Surface(l, b, h)
Surface = 2 * (l * b + b * h + h * l)
END FUNCTION
Average of three numbers
DECLARE FUNCTION Average(a, b, c)
CLS
INPUT "Enter three numbers: ", x, y, z
PRINT "Average = "; Average(x, y, z)
END
FUNCTION Average(a, b, c)
Average = (a + b + c) / 3
END FUNCTION
Celsius to Fahrenheit
DECLARE FUNCTION Fahrenheit(C)
CLS
INPUT "Enter temperature in Celsius: ", C
PRINT "In Fahrenheit = "; Fahrenheit(C)
END
FUNCTION Fahrenheit(C)
Fahrenheit = (9 * C / 5) + 32
END FUNCTION
Reverse a word
DECLARE FUNCTION ReverseWord(W$)
CLS
INPUT "Enter a word: ", W$
PRINT "Reverse: "; ReverseWord(W$)
END
FUNCTION ReverseWord(W$)
FOR i = LEN(W$) TO 1 STEP -1
R$ = R$ + MID$(W$, i, 1)
NEXT i
ReverseWord = R$
END FUNCTION
Count total number of consonants
DECLARE FUNCTION CountConsonant(S$)
CLS
INPUT "Enter a string: ", S$
PRINT "Total consonants: "; CountConsonant(S$)
END
FUNCTION CountConsonant(S$)
S$ = UCASE$(S$)
FOR i = 1 TO LEN(S$)
C$ = MID$(S$, i, 1)
IF C$ >= "A" AND C$ <= "Z" THEN
IF INSTR("AEIOU", C$) = 0 THEN Count = Count + 1
END IF
NEXT i
CountConsonant = Count
END FUNCTION
Area of a circle
DECLARE FUNCTION Area(r)
CLS
INPUT "Enter radius: ", r
PRINT "Area = "; Area(r)
END
FUNCTION Area(r)
Area = 3.1416 * r * r
END FUNCTION
Factorial of a number
DECLARE FUNCTION Factorial(n)
CLS
INPUT "Enter a number: ", n
PRINT "Factorial = "; Factorial(n)
END
FUNCTION Factorial(n)
f = 1
FOR i = 1 TO n
f = f * i
NEXT i
Factorial = f
END FUNCTION
Greater of two numbers
DECLARE FUNCTION Greater(a, b)
CLS
INPUT "Enter two numbers: ", x, y
PRINT "Greater number is "; Greater(x, y)
END
FUNCTION Greater(a, b)
IF a > b THEN
Greater = a
ELSE
Greater = b
END IF
END FUNCTION