0% found this document useful (0 votes)
9 views7 pages

QBASIC Function Programs Explained

Qb

Uploaded by

hatodininja458
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

QBASIC Function Programs Explained

Qb

Uploaded by

hatodininja458
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SEE Exam QBASIC Function Programs - Solved

Sum of Two Numbers


DECLARE FUNCTION Sum(A, B)
CLS
INPUT "Enter first number: ", X
INPUT "Enter second number: ", Y
PRINT "The sum is: "; Sum(X, Y)
END

FUNCTION Sum(A, B)
Sum = A + B
END FUNCTION

Area of Circle
DECLARE FUNCTION Area(R)
CLS
INPUT "Enter radius: ", R
PRINT "Area of circle = "; Area(R)
END

FUNCTION Area(R)
Area = 3.1416 * R * R
END FUNCTION

Simple Interest
DECLARE FUNCTION SI(P, T, R)
CLS
INPUT "Enter Principal: ", P
INPUT "Enter Time (in years): ", T
INPUT "Enter Rate: ", R
PRINT "Simple Interest = "; SI(P, T, R)
END

FUNCTION SI(P, T, R)
SI = (P * T * R) / 100
END FUNCTION

Largest Among Three Numbers


DECLARE FUNCTION Largest(A, B, C)
CLS
INPUT "Enter three numbers: ", A, B, C
PRINT "Largest = "; Largest(A, B, C)
END

FUNCTION Largest(A, B, C)
IF A > B AND A > C THEN
Largest = A
ELSEIF B > C THEN
Largest = B
ELSE
Largest = C
END IF
END FUNCTION

Even or Odd
DECLARE FUNCTION CheckEvenOdd(N$)
CLS
INPUT "Enter a number: ", N
PRINT CheckEvenOdd(N)
END

FUNCTION CheckEvenOdd(N)
IF N MOD 2 = 0 THEN
CheckEvenOdd = "Even"
ELSE
CheckEvenOdd = "Odd"
END IF
END FUNCTION

Sum of Digits
DECLARE FUNCTION SumDigits(N)
CLS
INPUT "Enter a number: ", N
PRINT "Sum of digits = "; SumDigits(N)
END

FUNCTION SumDigits(N)
S = 0
WHILE N > 0
R = N MOD 10
S = S + R
N = N \ 10
WEND
SumDigits = S
END FUNCTION

Factorial
DECLARE FUNCTION Fact(N)
CLS
INPUT "Enter a number: ", N
PRINT "Factorial = "; Fact(N)
END

FUNCTION Fact(N)
F = 1
FOR I = 1 TO N
F = F * I
NEXT
Fact = F
END FUNCTION

Prime or Composite
DECLARE FUNCTION PrimeCheck(N$)
CLS
INPUT "Enter a number: ", N
PRINT PrimeCheck(N)
END

FUNCTION PrimeCheck(N)
C = 0
FOR I = 1 TO N
IF N MOD I = 0 THEN C = C + 1
NEXT
IF C = 2 THEN
PrimeCheck = "Prime"
ELSE
PrimeCheck = "Composite"
END IF
END FUNCTION

Palindrome Number
DECLARE FUNCTION PalindromeCheck(N$)
CLS
INPUT "Enter a number: ", N
PRINT PalindromeCheck(N)
END

FUNCTION PalindromeCheck(N)
Temp = N
Rev = 0
WHILE N > 0
R = N MOD 10
Rev = Rev * 10 + R
N = N \ 10
WEND
IF Temp = Rev THEN
PalindromeCheck = "Palindrome"
ELSE
PalindromeCheck = "Not Palindrome"
END IF
END FUNCTION

Reverse a String
DECLARE FUNCTION ReverseStr(T$)
CLS
INPUT "Enter a word: ", W$
PRINT "Reverse: "; ReverseStr(W$)
END

FUNCTION ReverseStr(T$)
R$ = ""
FOR I = LEN(T$) TO 1 STEP -1
R$ = R$ + MID$(T$, I, 1)
NEXT
ReverseStr = R$
END FUNCTION

Count Vowels
DECLARE FUNCTION CountVowel(T$)
CLS
INPUT "Enter a word: ", W$
PRINT "Number of vowels: "; CountVowel(W$)
END

FUNCTION CountVowel(T$)
C = 0
T$ = UCASE$(T$)
FOR I = 1 TO LEN(T$)
CH$ = MID$(T$, I, 1)
IF CH$ = "A" OR CH$ = "E" OR CH$ = "I" OR CH$ = "O" OR CH$ = "U" THEN
C = C + 1
END IF
NEXT
CountVowel = C
END FUNCTION

Count Words in Sentence


DECLARE FUNCTION CountWords(S$)
CLS
INPUT "Enter a sentence: ", ST$
PRINT "Number of words: "; CountWords(ST$)
END

FUNCTION CountWords(S$)
C = 1
FOR I = 1 TO LEN(S$)
IF MID$(S$, I, 1) = " " THEN C = C + 1
NEXT
CountWords = C
END FUNCTION

HCF of Two Numbers


DECLARE FUNCTION HCF(A, B)
CLS
INPUT "Enter two numbers: ", X, Y
PRINT "HCF = "; HCF(X, Y)
END

FUNCTION HCF(A, B)
WHILE B <> 0
R = A MOD B
A = B
B = R
WEND
HCF = A
END FUNCTION

LCM of Two Numbers


DECLARE FUNCTION LCM(A, B)
CLS
INPUT "Enter two numbers: ", X, Y
PRINT "LCM = "; LCM(X, Y)
END

FUNCTION LCM(A, B)
LCM = (A * B) / HCF(A, B)
END FUNCTION

FUNCTION HCF(A, B)
WHILE B <> 0
R = A MOD B
A = B
B = R
WEND
HCF = A
END FUNCTION

Decimal to Binary
DECLARE FUNCTION DecToBin$(N)
CLS
INPUT "Enter decimal number: ", N
PRINT "Binary = "; DecToBin$(N)
END

FUNCTION DecToBin$(N)
B$ = ""
WHILE N > 0
R = N MOD 2
B$ = LTRIM$(STR$(R)) + B$
N = N \ 2
WEND
DecToBin$ = B$
END FUNCTION

Binary to Decimal
DECLARE FUNCTION BinToDec(N$)
CLS
INPUT "Enter binary number: ", B$
PRINT "Decimal = "; BinToDec(B$)
END

FUNCTION BinToDec(N$)
L = LEN(N$)
D = 0
FOR I = 1 TO L
D = D + VAL(MID$(N$, I, 1)) * 2 ^ (L - I)
NEXT
BinToDec = D
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

Fibonacci Series
DECLARE FUNCTION Fibonacci(N)
CLS
INPUT "Enter number of terms: ", N
PRINT "Fibonacci series:"
FOR I = 1 TO N
PRINT Fibonacci(I);
NEXT
END

FUNCTION Fibonacci(N)
IF N = 1 THEN
Fibonacci = 0
ELSEIF N = 2 THEN
Fibonacci = 1
ELSE
Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
END IF
END FUNCTION

Armstrong Number
DECLARE FUNCTION ArmstrongCheck(N$)
CLS
INPUT "Enter a number: ", N
PRINT ArmstrongCheck(N)
END

FUNCTION ArmstrongCheck(N)
Temp = N
S = 0
L = LEN(STR$(N)) - 1
WHILE N > 0
R = N MOD 10
S = S + R ^ L
N = N \ 10
WEND
IF Temp = S THEN
ArmstrongCheck = "Armstrong"
ELSE
ArmstrongCheck = "Not Armstrong"
END IF
END FUNCTION

Area of Four Walls


DECLARE FUNCTION AreaWalls(L, B, H)
CLS
INPUT "Enter length: ", L
INPUT "Enter breadth: ", B
INPUT "Enter height: ", H
PRINT "Area of four walls = "; AreaWalls(L, B, H)
END

FUNCTION AreaWalls(L, B, H)
AreaWalls = 2 * H * (L + B)
END FUNCTION

You might also like