Are you a student preparing for your Class 10 Computer exam? Are you seeking a comprehensive resource to master QBASIC Function...End Function programming? Look no further! In this article, we'll delve into some essential QBASIC Function...End Function questions along with their corresponding code snippets.
QBASIC, a structured programming language, is often included in the curriculum of Class 10 Computer Science courses. Understanding its fundamentals and mastering its syntax can significantly boost your confidence when tackling exam questions.
Let's explore some important QBASIC Function...End Function questions and their solutions:
Very Important Function....End Function QBASIC Programming(Turn on Desktop Mode for Better View)
1. To calculate factorial of a given number:(using Function...End Function)
DECLARE FUNCTION FACTORIAL (N) CLS INPUT "Enter a number: ", N PRINT "Factorial of"; N; "is"; FACTORIAL(N) FUNCTION FACTORIAL (N) IF N = 0 THEN FACTORIAL = 1 ELSE FACTORIAL = 1 FOR I = 1 TO N FACTORIAL = FACTORIAL * I NEXT I END IF END FUNCTION
2. To calculate the average of three numbers:(using Function...End Function)
DECLARE FUNCTION AVERAGE (A, B, C) CLS INPUT "Enter three numbers: ", A, B, C PRINT "Average is"; AVERAGE(A, B, C) FUNCTION AVERAGE (A, B, C) AVERAGE = (A + B + C) / 3 END FUNCTION
3. To find the longest string among three strings:
DECLARE FUNCTION LONGEST_STRING (S1, S2, S3) CLS INPUT "Enter three strings: ", S1, S2, S3 PRINT "Longest string is: "; LONGEST_STRING(S1, S2, S3) FUNCTION LONGEST_STRING (S1, S2, S3) IF LEN(S1) >= LEN(S2) AND LEN(S1) >= LEN(S3) THEN LONGEST_STRING = S1 ELSEIF LEN(S2) >= LEN(S1) AND LEN(S2) >= LEN(S3) THEN LONGEST_STRING = S2 ELSE LONGEST_STRING = S3 END IF END FUNCTION
4. To check whether the given number is positive, negative, or zero:
DECLARE FUNCTION CHECK_NUMBER (N) CLS INPUT "Enter a number: ", N PRINT CHECK_NUMBER(N) FUNCTION CHECK_NUMBER (N) IF N > 0 THEN CHECK_NUMBER = "Positive" ELSEIF N < 0 THEN CHECK_NUMBER = "Negative" ELSE CHECK_NUMBER = "Zero" END IF END FUNCTION
5. To calculate the sum of digits of a given number:
DECLARE FUNCTION SUM_OF_DIGITS (N) CLS INPUT "Enter a number: ", N PRINT "Sum of digits is: "; SUM_OF_DIGITS(N) FUNCTION SUM_OF_DIGITS (N) SUM = 0 WHILE N > 0 REM Extract the last digit DIGIT = N MOD 10 SUM = SUM + DIGIT N = N \ 10 WEND SUM_OF_DIGITS = SUM END FUNCTION
6. To calculate the product of digits of a given number:
DECLARE FUNCTION PRODUCT_OF_DIGITS (N) CLS INPUT "Enter a number: ", N PRINT "Product of digits is: "; PRODUCT_OF_DIGITS(N) FUNCTION PRODUCT_OF_DIGITS (N) PRODUCT = 1 WHILE N > 0 REM Extract the last digit DIGIT = N MOD 10 PRODUCT = PRODUCT * DIGIT N = N \ 10 WEND PRODUCT_OF_DIGITS = PRODUCT END FUNCTION
7. To convert Celsius temperature scale into Fahrenheit temperature scale. [ (C-0)/100 =(F-32)/180 ]
DECLARE FUNCTION TEMP (C) CLS INPUT "Enter temperature in Celsius";C X = TEMP (C) PRINT "Equivalent Fahrenheit temperature is";X END FUNCTION TEMP (C) F= (18*C+320)/10 TEMP = F END FUNCTION
8. To convert Nepali rupee into Indian currency . [ 1IC = 1.6Rs ]
DECLARE FUNCTION CONVERT (N) CLS INPUT "Enter Nepali Rupee";N X = CONVERT (N) PRINT "Equivalent Indian currency is";X END FUNCTION CONVERT (N) IC = N/1.6 CONVERT = IC END FUNCTION
9. To calculate sum of n-natural number.
DECLARE FUNCTION SUM (N) CLS INPUT "Enter Number";N X = SUM (N) PRINT "Sum is:" ; X END FUNCTION SUM (N) FOR I = 1 TO N S = S+I NEXT I SUM = S END FUNCTION
10. To check whether the given number is palindrome or not:
DECLARE FUNCTION IS_PALINDROME (N) CLS INPUT "Enter a number: ", N IF IS_PALINDROME(N) THEN PRINT N; " is a palindrome" ELSE PRINT N; " is not a palindrome" END IF FUNCTION IS_PALINDROME (N) ORIGINAL_N = N REVERSED_N = 0 WHILE N > 0 DIGIT = N MOD 10 REVERSED_N = REVERSED_N * 10 + DIGIT N = N \ 10 WEND IF ORIGINAL_N = REVERSED_N THEN IS_PALINDROME = -1 ELSE IS_PALINDROME = 0 END IF END FUNCTION
11. To check whether the given number is Armstrong or not:
DECLARE FUNCTION IS_ARMSTRONG (N) CLS INPUT "Enter a number: ", N IF IS_ARMSTRONG(N) THEN PRINT N; " is an Armstrong number" ELSE PRINT N; " is not an Armstrong number" END IF FUNCTION IS_ARMSTRONG (N) TEMP = N ARMSTRONG_SUM = 0 WHILE TEMP > 0 DIGIT = TEMP MOD 10 ARMSTRONG_SUM = ARMSTRONG_SUM + DIGIT ^ 3 TEMP = TEMP \ 10 WEND IF N = ARMSTRONG_SUM THEN IS_ARMSTRONG = -1 ELSE IS_ARMSTRONG = 0 END IF END FUNCTION
12. To check whether the given number is prime or composite:
DECLARE FUNCTION IS_PRIME (N) CLS INPUT "Enter a number: ", N IF IS_PRIME(N) THEN PRINT N; " is a prime number" ELSE PRINT N; " is a composite number" END IF FUNCTION IS_PRIME (N) IF N <= 1 THEN IS_PRIME = 0 EXIT FUNCTION END IF FOR I = 2 TO INT(SQR(N)) IF N MOD I = 0 THEN IS_PRIME = 0 EXIT FUNCTION END IF NEXT I IS_PRIME = -1 END FUNCTION
13. To calculate the sum of even digits from a given number:
DECLARE FUNCTION SUM_OF_EVEN_DIGITS (N) CLS INPUT "Enter a number: ", N PRINT "Sum of even digits is: "; SUM_OF_EVEN_DIGITS(N) FUNCTION SUM_OF_EVEN_DIGITS (N) SUM = 0 WHILE N > 0 DIGIT = N MOD 10 IF DIGIT MOD 2 = 0 THEN SUM = SUM + DIGIT END IF N = N \ 10 WEND SUM_OF_EVEN_DIGITS = SUM END FUNCTION
14. To calculate the product of odd digits from a given number:
DECLARE FUNCTION PRODUCT_OF_ODD_DIGITS (N) CLS INPUT "Enter a number: ", N PRINT "Product of odd digits is: "; PRODUCT_OF_ODD_DIGITS(N) FUNCTION PRODUCT_OF_ODD_DIGITS (N) PRODUCT = 1 WHILE N > 0 DIGIT = N MOD 10 IF DIGIT MOD 2 <> 0 THEN PRODUCT = PRODUCT * DIGIT END IF N = N \ 10 WEND PRODUCT_OF_ODD_DIGITS = PRODUCT END FUNCTION
To check whether the given string is palindrome or not:
DECLARE FUNCTION IS_STRING_PALINDROME (STR$) CLS INPUT "Enter a string: ", STR$ IF IS_STRING_PALINDROME(STR$) THEN PRINT STR$; " is a palindrome" ELSE PRINT STR$; " is not a palindrome" END IF FUNCTION IS_STRING_PALINDROME (STR$) LEN_STR = LEN(STR$) IS_PALINDROME = -1 FOR I = 1 TO LEN_STR \ 2 IF MID$(STR$, I, 1) <> MID$(STR$, LEN_STR - I + 1, 1) THEN IS_PALINDROME = 0 EXIT FUNCTION END IF NEXT I IS_STRING_PALINDROME = IS_PALINDROME END FUNCTION
16. To input a string and count how many times the first letter of the same string repeats:
DECLARE FUNCTION COUNT_FIRST_LETTER (STR$) CLS INPUT "Enter a string: ", STR$ PRINT "The first letter repeats "; COUNT_FIRST_LETTER(STR$); " times." FUNCTION COUNT_FIRST_LETTER (STR$) FIRST_LETTER = UCASE$(LEFT$(STR$, 1)) COUNT = 0 FOR I = 1 TO LEN(STR$) IF UCASE$(MID$(STR$, I, 1)) = FIRST_LETTER THEN COUNT = COUNT + 1 END IF NEXT I COUNT_FIRST_LETTER = COUNT END FUNCTION
17. To count total numbers of vowels in a given string:
DECLARE FUNCTION COUNT_VOWELS (STR$) CLS INPUT "Enter a string: ", STR$ PRINT "Total vowels: "; COUNT_VOWELS(STR$) FUNCTION COUNT_VOWELS (STR$) VOWELS = 0 FOR I = 1 TO LEN(STR$) SELECT CASE UCASE$(MID$(STR$, I, 1)) CASE "A", "E", "I", "O", "U" VOWELS = VOWELS + 1 END SELECT NEXT I COUNT_VOWELS = VOWELS END FUNCTION
18. To display all the consonants from a given string:
DECLARE FUNCTION DISPLAY_CONSONANTS (STR$) CLS INPUT "Enter a string: ", STR$ PRINT "Consonants: "; DISPLAY_CONSONANTS(STR$) FUNCTION DISPLAY_CONSONANTS (STR$) CONSONANTS$ = "" FOR I = 1 TO LEN(STR$) CHAR$ = UCASE$(MID$(STR$, I, 1)) IF INSTR("AEIOU", CHAR$) = 0 AND CHAR$ >= "A" AND CHAR$ <= "Z" THEN CONSONANTS$ = CONSONANTS$ + CHAR$ END IF NEXT I DISPLAY_CONSONANTS = CONSONANTS$ END FUNCTION
19. To display a given string into alternative capital and small letters, such as "NePaL" -> "nEpAl"
DECLARE FUNCTION ALTERNATE_CASE (STR$) CLS INPUT "Enter a string: ", STR$ PRINT "Alternate case: "; ALTERNATE_CASE(STR$) FUNCTION ALTERNATE_CASE (STR$) NEW_STR$ = "" FOR I = 1 TO LEN(STR$) IF I MOD 2 = 1 THEN NEW_STR$ = NEW_STR$ + UCASE$(MID$(STR$, I, 1)) ELSE NEW_STR$ = NEW_STR$ + LCASE$(MID$(STR$, I, 1)) END IF NEXT I ALTERNATE_CASE = NEW_STR$ END FUNCTION
Tags
If you have any doubts, Please let me know