Modular Programming( Class 10 )

⌘K
  1. Home
  2. Docs
  3. Modular Programming( Clas...
  4. Exercise
  5. All Exercise

All Exercise

1. Answer the following Questions in one sentences:

a. What is modular programming?

Ans: Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, each of which contains everything necessary to execute one aspect of the desired functionality.

b. What is procedure?

Ans: A procedure is a set of coded instructions that tell a computer how to run a program or calculation.

c. List the type of sub modular used in QBASIC.

Ans: The types of sub modular used in QBASIC are SUB procedures and FUNCTION procedures.

d. Define library function.

Ans: A library function is a precompiled, prewritten function that is built into the programming language and can be used in programs to perform common tasks.

e. Define sub procedure.

Ans: A sub procedure in QBASIC is a block of code that performs a specific task but does not return a value to the calling code.

f. Define argument.

Ans: An argument is a value that is passed to a procedure or function at the time of calling it, which the procedure or function can use within its execution.

g. What is argument passing by reference method?

Ans: Argument passing by reference method is a way of passing arguments to a procedure or function where the called procedure can modify the variable’s value in the calling environment.

2. Write short answer of the following questions:

a. Mention any four advantages of modular programming.

Ans: Four advantages of modular programming are:

‣ Improved Readability and Maintainability: Modular programming allows complex programs to be broken down into smaller, manageable modules, making the code easier to read, understand, and maintain.

‣ Reusability: Modules can be reused across different programs, reducing redundancy and saving development time.

‣ Simplified Debugging and Testing: Each module can be tested and debugged independently, which makes it easier to identify and fix errors.

‣ Collaborative Development: Different team members can work on separate modules simultaneously, facilitating parallel development and speeding up the overall process.

b. Mention any two differences between sub procedure and function procedure.

Ans: Two differences between sub procedure and function procedure:

Return Value: A function procedure returns a value to the calling code, whereas a sub procedure does not return a value.

Usage: Functions are typically used for calculations or operations that need to return a result, while sub procedures are used for performing actions or operations where a return value is not necessary.

c. Write the differences between argument and parameter.

Ans: The differences between argument and parameter are:

Definition:

  • Parameter: A parameter is a variable used in a function definition to represent the value that will be passed to the function. It acts as a placeholder.
  • Argument: An argument is the actual value or expression that is passed to the function when it is called.

Location:

  • Parameter: Parameters appear in the function signature, inside the parentheses when the function is defined.
  • Argument: Arguments appear in the function call, inside the parentheses when the function is invoked.

d. Write the differences between argument passing by reference method and value method.

Ans: The differences between argument passing by reference method and value method are:

Definition:

  • Passing by Value: When an argument is passed by value, a copy of the actual value is passed to the function. Changes made to the parameter inside the function do not affect the original argument.
  • Passing by Reference: When an argument is passed by reference, a reference (or address) of the actual value is passed to the function. Changes made to the parameter inside the function directly affect the original argument.

Memory:

  • Passing by Value: Uses more memory because a separate copy of the variable is created.
  • Passing by Reference: Uses less memory since no new copy of the variable is created; instead, a reference to the existing variable is used.

e. Mention the difference between local variable and global variable.

Ans: The difference between local variable and global variable are:

Scope:

  • Local Variable: A local variable is declared inside a function or a block and is only accessible within that function or block.
  • Global Variable: A global variable is declared outside of all functions and is accessible from any part of the program.

Lifetime:

  • Local Variable: Exists only during the execution of the function or block in which it is declared. It is destroyed when the function or block ends.
  • Global Variable: Exists for the entire duration of the program. It is created when the program starts and is destroyed when the program ends.

3. Write a program to define sub procedure to display sum of any three numbers. The program allows a user to input three numbers in the main module.

Ans:

DECLARE SUB DisplaySum (a AS INTEGER, b AS INTEGER, c AS INTEGER)

CLS
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
INPUT "Enter third number: ", num3

CALL DisplaySum(num1, num2, num3)

SUB DisplaySum (a AS INTEGER, b AS INTEGER, c AS INTEGER)
    sum = a + b + c
    PRINT "The sum of the three numbers is "; sum
END SUB

4. Write a program using SUB…. END SUB to display temperature in Celsius of a temperature input in Fahrenheit. [ Hint: Celsius = 5 (Fahrenheit – 32)/9 ]

Ans:

DECLARE SUB FahrenheitToCelsius (f AS SINGLE)

CLS
INPUT "Enter temperature in Fahrenheit: ", tempF

CALL FahrenheitToCelsius(tempF)

SUB FahrenheitToCelsius (f AS SINGLE)
    c = 5 * (f - 32) / 9
    PRINT "The temperature in Celsius is "; c
END SUB

5. Write a program to define a sub procedure to display simple interest where a user inputs the required data in the main procedure.

Ans:

DECLARE SUB DisplaySimpleInterest (p AS SINGLE, r AS SINGLE, t AS SINGLE)

CLS
INPUT "Enter principal amount: ", principal
INPUT "Enter rate of interest: ", rate
INPUT "Enter time period in years: ", time

CALL DisplaySimpleInterest(principal, rate, time)

SUB DisplaySimpleInterest (p AS SINGLE, r AS SINGLE, t AS SINGLE)
    simpleInterest = (p * r * t) / 100
    PRINT "The simple interest is "; simpleInterest
END SUB

6. Write a program to define a sub procedure to display the area of circle. This program allows a ser inputs the required data in the main procedure.

Ans:

DECLARE SUB DisplayAreaOfCircle (r AS SINGLE)

CLS
INPUT "Enter the radius of the circle: ", radius

CALL DisplayAreaOfCircle(radius)

SUB DisplayAreaOfCircle (r AS SINGLE)
    area = 3.14159 * r * r
    PRINT "The area of the circle is "; area
END SUB

7. Write a program to declare a sub procedure to display perimeter of a rectangle.

Ans:

DECLARE SUB DisplayPerimeter (l AS SINGLE, w AS SINGLE)

CLS
INPUT "Enter the length of the rectangle: ", length
INPUT "Enter the width of the rectangle: ", width

CALL DisplayPerimeter(length, width)

SUB DisplayPerimeter (l AS SINGLE, w AS SINGLE)
    perimeter = 2 * (l + w)
    PRINT "The perimeter of the rectangle is "; perimeter
END SUB

8. Write a program to define a sub procedure to display the area of sphere. The program allows a user to input the radius of a sphere in the main module.[ area of sphere =4piR^2 ]

Ans:

DECLARE SUB DisplayAreaOfSphere (r AS SINGLE)

CLS
INPUT "Enter the radius of the sphere: ", radius

CALL DisplayAreaOfSphere(radius)

SUB DisplayAreaOfSphere (r AS SINGLE)
    area = 4 * 3.14159 * r * r
    PRINT "The area of the sphere is "; area
END SUB

9. Write a program using SUB… END SUB to display the highest number among any three numbers input by a user in the main module.

Ans:

DECLARE SUB DisplayHighestNumber (a AS INTEGER, b AS INTEGER, c AS INTEGER)

CLS
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
INPUT "Enter third number: ", num3

CALL DisplayHighestNumber(num1, num2, num3)

SUB DisplayHighestNumber (a AS INTEGER, b AS INTEGER, c AS INTEGER)
    IF a > b AND a > c THEN
        highest = a
    ELSEIF b > a AND b > c THEN
        highest = b
    ELSE
        highest = c
    END IF
    PRINT "The highest number is "; highest
END SUB

10. Write a program to declare a sub procedure that displays the longest name among any three names input by a user in the main module.

Ans:

DECLARE SUB DisplayLongestName (name1 AS STRING, name2 AS STRING, name3 AS STRING)

CLS
INPUT "Enter first name: ", firstName$
INPUT "Enter second name: ", secondName$
INPUT "Enter third name: ", thirdName$

CALL DisplayLongestName(firstName$, secondName$, thirdName$)

SUB DisplayLongestName (name1 AS STRING, name2 AS STRING, name3 AS STRING)
    IF LEN(name1) > LEN(name2) AND LEN(name1) > LEN(name3) THEN
        longestName = name1
    ELSEIF LEN(name2) > LEN(name1) AND LEN(name2) > LEN(name3) THEN
        longestName = name2
    ELSE
        longestName = name3
    END IF
    PRINT "The longest name is "; longestName
END SUB

11. Write a program to define a sub procedure that displays number of vowels and constants characters of a word input by a user in the main module.

Ans:

DECLARE SUB CountVowelsConsonants (W$)
CLS
INPUT "Enter a word: ", word$
CALL CountVowelsConsonants(word$)
END

SUB CountVowelsConsonants (W$)
    vowels = 0
    consonants = 0
    FOR i = 1 TO LEN(W$)
        ch$ = MID$(W$, i, 1)
        SELECT CASE ch$
            CASE "A", "E", "I", "O", "U", "a", "e", "i", "o", "u"
                vowels = vowels + 1
            CASE ELSE
                consonants = consonants + 1
        END SELECT
    NEXT i
    PRINT "Number of vowels: "; vowels
    PRINT "Number of consonants: "; consonants
END SUB

12. Write a program to define a sub procedure Vowel(W$) to remove consonant characters from the supplied word and display the new word without consonant characters. [ For example: KATHMANDU should be displayed as AAU]

Ans:

DECLARE SUB Vowel (W$)
CLS
INPUT "Enter a word: ", word$
CALL Vowel(word$)
END

SUB Vowel (W$)
    newWord$ = ""
    FOR i = 1 TO LEN(W$)
        ch$ = MID$(W$, i, 1)
        IF ch$ = "A" OR ch$ = "E" OR ch$ = "I" OR ch$ = "O" OR ch$ = "U" OR ch$ = "a" OR ch$ = "e" OR ch$ = "i" OR ch$ = "o" OR ch$ = "u" THEN
            newWord$ = newWord$ + ch$
        END IF
    NEXT i
    PRINT "Word without consonants: "; newWord$
END SUB

13. Write a program to define a sub procedure Consonant(W$) to remove vowel characters and displays a word without vowel characters . [ For example: NEPAL should be displayed as NPL]

Ans:

DECLARE SUB Consonant (W$)
CLS
INPUT "Enter a word: ", word$
CALL Consonant(word$)
END

SUB Consonant (W$)
    newWord$ = ""
    FOR i = 1 TO LEN(W$)
        ch$ = MID$(W$, i, 1)
        IF NOT (ch$ = "A" OR ch$ = "E" OR ch$ = "I" OR ch$ = "O" OR ch$ = "U" OR ch$ = "a" OR ch$ = "e" OR ch$ = "i" OR ch$ = "o" OR ch$ = "u") THEN
            newWord$ = newWord$ + ch$
        END IF
    NEXT i
    PRINT "Word without vowels: "; newWord$
END SUB

14. Write a program to define a sub procedure to reverse a word input by a user in the main module.

Ans:

DECLARE SUB ReverseWord (W$)
CLS
INPUT "Enter a word: ", word$
CALL ReverseWord(word$)
END

SUB ReverseWord (W$)
    reversedWord$ = ""
    FOR i = LEN(W$) TO 1 STEP -1
        reversedWord$ = reversedWord$ + MID$(W$, i, 1)
    NEXT i
    PRINT "Reversed word: "; reversedWord$
END SUB

15. Write a program to define a sub procedure to check and display whether the word supplied by a user in the main module is palindrome or not.

Ans:

DECLARE SUB CheckPalindrome (W$)
CLS
INPUT "Enter a word: ", word$
CALL CheckPalindrome(word$)
END

SUB CheckPalindrome (W$)
    reversedWord$ = ""
    FOR i = LEN(W$) TO 1 STEP -1
        reversedWord$ = reversedWord$ + MID$(W$, i, 1)
    NEXT i
    IF UCASE$(W$) = UCASE$(reversedWord$) THEN
        PRINT "The word is a palindrome."
    ELSE
        PRINT "The word is not a palindrome."
    END IF
END SUB

16. Write a program to declare a sub procedure that displays HCF of any two integers.

Ans:

DECLARE SUB HCF (A, B)
CLS
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
CALL HCF(num1, num2)
END

SUB HCF (A, B)
    WHILE A <> B
        IF A > B THEN
            A = A - B
        ELSE
            B = B - A
        END IF
    WEND
    PRINT "HCF is: "; A
END SUB

17. Write a program to define a sub procedure to display sum of numbers from 15 to 45.

Ans:

DECLARE SUB SumNumbers ()
CLS
CALL SumNumbers
END

SUB SumNumbers
    sum = 0
    FOR i = 15 TO 45
        sum = sum + i
    NEXT i
    PRINT "Sum of numbers from 15 to 45 is: "; sum
END SUB

18. Write a program to define a sub procedure to display the numeric series as 2, 3, 5, 8, … up to 15 terms.

Ans:

DECLARE SUB NumericSeries ()
CLS
CALL NumericSeries
END

SUB NumericSeries
    a = 2
    b = 3
    PRINT a
    PRINT b
    FOR i = 3 TO 15
        c = a + b
        PRINT c
        a = b
        b = c
    NEXT i
END SUB

19. Write a program to declare a sub procedure to generate a number series as 9, 16, up to 12 terms.

Ans:

DECLARE SUB NumberSeries ()
CLS
CALL NumberSeries
END

SUB NumberSeries
    term = 9
    FOR i = 1 TO 12
        PRINT term
        term = term + 7
    NEXT i
END SUB

20. Write a program to declare a sub procedure to generate a number series as 7, 11, 16, … up to 13th term.

Ans:

DECLARE SUB GenerateSeries ()
CLS
CALL GenerateSeries
END

SUB GenerateSeries
    term = 7
    diff = 4
    FOR i = 1 TO 13
        PRINT term
        term = term + diff
        diff = diff + 1
    NEXT i
END SUB

21. Write a program using SUB.. END SUB to display the following string patterns:

a.

KATHMANDU
ATHMANDU
THMAN
HMA
M

Ans:

DECLARE SUB DisplayPatternA (str$)

CLS
CALL DisplayPatternA("KATHMANDU")
END

SUB DisplayPatternA (str$)
    FOR i = 1 TO LEN(str$)
        PRINT MID$(str$, i)
    NEXT i
END SUB

b.

M
HMA
THMAN
ATHMAND
KATHMANDU

Ans:

DECLARE SUB DisplayPatternB (str$)

CLS
CALL DisplayPatternB("KATHMANDU")
END

SUB DisplayPatternB (str$)
    pattern1$ = "M"
    pattern2$ = "HMA"
    pattern3$ = "THMAN"
    pattern4$ = "ATHMAND"
    pattern5$ = "KATHMANDU"
    
    PRINT pattern1$
    PRINT pattern2$
    PRINT pattern3$
    PRINT pattern4$
    PRINT pattern5$
END SUB

c.

WELCOME
ELCOME
LCOME
COME
OME
ME
E

Ans:

DECLARE SUB DisplayPatternC (str$)

CLS
CALL DisplayPatternC("WELCOME")
END

SUB DisplayPatternC (str$)
    FOR i = 1 TO LEN(str$)
        PRINT MID$(str$, i)
    NEXT i
END SUB

d.

E
ME
OME
COME
LCOME
ELCOME
WELCOME

Ans:

DECLARE SUB DisplayPatternD (str$)

CLS
CALL DisplayPatternD("WELCOME")
END

SUB DisplayPatternD (str$)
    FOR i = LEN(str$) TO 1 STEP -1
        PRINT MID$(str$, i)
    NEXT i
END SUB

22. Write a program to declare a sub procedure to display factors of an input number.

Ans:

DECLARE SUB DisplayFactors (n)

INPUT "Enter a number: ", num
CALL DisplayFactors(num)

SUB DisplayFactors (n)
    PRINT "Factors of"; n; "are:";
    FOR i = 1 TO n
        IF n MOD i = 0 THEN
            PRINT i;
        END IF
    NEXT i
    PRINT
END SUB

23. Write a program to declare a sub procedure that checks whether a number input by a user in the main module is prime or composite.

Ans:

DECLARE SUB CheckPrimeComposite (n)

INPUT "Enter a number: ", num
CALL CheckPrimeComposite(num)

SUB CheckPrimeComposite (n)
    isPrime = 1
    IF n < 2 THEN
        isPrime = 0
    ELSE
        FOR i = 2 TO n / 2
            IF n MOD i = 0 THEN
                isPrime = 0
                EXIT FOR
            END IF
        NEXT i
    END IF
    IF isPrime = 1 THEN
        PRINT n; "is a Prime number."
    ELSE
        PRINT n; "is a Composite number."
    END IF
END SUB

24. Write a program using SUB… END SUB to reverse an integer number input by a user.

Ans:

DECLARE SUB ReverseNumber (n)

INPUT "Enter a number: ", num
CALL ReverseNumber(num)

SUB ReverseNumber (n)
    reversed = 0
    WHILE n <> 0
        remainder = n MOD 10
        reversed = reversed * 10 + remainder
        n = n \ 10
    WEND
    PRINT "Reversed Number is: "; reversed
END SUB

25. Write a program using SUB… END SUB to display whether a number is palindrome or not.

Ans:

DECLARE SUB CheckPalindrome (n)

INPUT "Enter a number: ", num
CALL CheckPalindrome(num)

SUB CheckPalindrome (n)
    original = n
    reversed = 0
    WHILE n <> 0
        remainder = n MOD 10
        reversed = reversed * 10 + remainder
        n = n \ 10
    WEND
    IF original = reversed THEN
        PRINT original; "is a Palindrome."
    ELSE
        PRINT original; "is not a Palindrome."
    END IF
END SUB

26. Write a program using SUB… END SUB to display any ten numbers input by a user in scending order. The program allows a user to input ten numbers in the main module.

Ans:

DECLARE SUB SortNumbers (arr())

DIM numbers(10) AS INTEGER

FOR i = 1 TO 10
    INPUT "Enter number "; i; ": ", numbers(i)
NEXT i

CALL SortNumbers(numbers())

PRINT "Numbers in ascending order:"
FOR i = 1 TO 10
    PRINT numbers(i);
NEXT i
PRINT

SUB SortNumbers (arr())
    FOR i = 1 TO 9
        FOR j = i + 1 TO 10
            IF arr(i) > arr(j) THEN
                SWAP arr(i), arr(j)
            END IF
        NEXT j
    NEXT i
END SUB

27. Write a program using FUNCTION…. END FUNCTION statement to calculate and display area of rectangle.

Ans:

DECLARE FUNCTION AreaRectangle (length, width)

INPUT "Enter length of rectangle: ", length
INPUT "Enter width of rectangle: ", width

PRINT "Area of Rectangle: "; AreaRectangle(length, width)

FUNCTION AreaRectangle (length, width)
    AreaRectangle = length * width
END FUNCTION

28. Write a program using function procedure that displays area of circle. The program allows a user to input radius in the main module.

Ans:

DECLARE FUNCTION AreaCircle (radius)

INPUT "Enter radius of circle: ", radius

PRINT "Area of Circle: "; AreaCircle(radius)

FUNCTION AreaCircle (radius)
    PI = 3.14159
    AreaCircle = PI * radius * radius
END FUNCTION

29. Write a program using function procedure that calculates and displays volume of a box.

Ans:

DECLARE FUNCTION VolumeBox (length, width, height)

INPUT "Enter length of box: ", length
INPUT "Enter width of box: ", width
INPUT "Enter height of box: ", height

PRINT "Volume of Box: "; VolumeBox(length, width, height)

FUNCTION VolumeBox (length, width, height)
    VolumeBox = length * width * height
END FUNCTION

30. Write a program to define function procedure that returns simple interest. The program allows a user to input principal, rate and time in the main procedure.

Ans:

DECLARE FUNCTION SimpleInterest (principal, rate, time)

INPUT "Enter principal amount: ", principal
INPUT "Enter rate of interest: ", rate
INPUT "Enter time period: ", time

PRINT "Simple Interest: "; SimpleInterest(principal, rate, time)

FUNCTION SimpleInterest (principal, rate, time)
    SimpleInterest = (principal * rate * time) / 100
END FUNCTION

31. Write a program to display the smallest number among any three numbers input by a user in the main procedure using function procedure.

Ans:

DECLARE FUNCTION findSmallest (a AS INTEGER, b AS INTEGER, c AS INTEGER) AS INTEGER

CLS
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
INPUT "Enter third number: ", num3

PRINT "The smallest number is: "; findSmallest(num1, num2, num3)

FUNCTION findSmallest (a AS INTEGER, b AS INTEGER, c AS INTEGER) AS INTEGER
    smallest = a
    IF b < smallest THEN smallest = b
    IF c < smallest THEN smallest = c
    findSmallest = smallest
END FUNCTION

32. Write a program that defines a user defined function which checks whether a input number is positive, negative or zero.

Ans:

DECLARE SUB checkNumber (num AS INTEGER)

CLS
INPUT "Enter a number: ", number
checkNumber number

SUB checkNumber (num AS INTEGER)
    IF num > 0 THEN
        PRINT "The number is positive."
    ELSEIF num < 0 THEN
        PRINT "The number is negative."
    ELSE
        PRINT "The number is zero."
    END IF
END SUB

33. Write a program to store month of date of a computer system in a variable and defines a function which returns month in word.

Ans:

DIM monthNum AS INTEGER
CLS
monthNum = MONTH(DATE$)
PRINT "Month in word: "; getMonth(monthNum)
FUNCTION getMonth(monthNum)
    SELECT CASE monthNum
        CASE 1
            getMonth = "January"
        CASE 2
            getMonth = "February"
        CASE 3
            getMonth = "March"
        CASE 4
            getMonth = "April"
        CASE 5
            getMonth = "May"
        CASE 6
            getMonth = "June"
        CASE 7
            getMonth = "July"
        CASE 8
            getMonth = "August"
        CASE 9
            getMonth = "September"
        CASE 10
            getMonth = "October"
        CASE 11
            getMonth = "November"
        CASE 12
            getMonth = "December"
    END SELECT
END FUNCTION

34. Write a program to define a function procedure to reverse a given string. The program allows a user to input string in the main module and passes the string as argument to the function.

Ans:

CLS
INPUT "Enter a string: ", str
PRINT "Reversed string: "; reverseString(str)
FUNCTION reverseString(str)
    FOR i = LEN(str) TO 1 STEP -1
        reverseString = reverseString & MID$(str, i, 1)
    NEXT i
END FUNCTION

35. Write a program that defines a function procedure to check whether a three digits number is Armstrong or not.

Ans:

CLS
INPUT "Enter a three-digit number: ", num
IF isArmstrong(num) THEN
    PRINT num; " is an Armstrong number."
ELSE
    PRINT num; " is not an Armstrong number."
END IF
FUNCTION isArmstrong(num)
    temp = num
    sum = 0
    DO
        digit = temp MOD 10
        sum = sum + digit ^ 3
        temp = temp \ 10
    LOOP UNTIL temp = 0
    isArmstrong = (sum = num)
END FUNCTION

36. Write a program that defines a function procedure which returns whether input number is prime or composite.

Ans:

CLS
INPUT "Enter a number: ", num
IF isPrime(num) THEN
    PRINT num; " is a prime number."
ELSE
    PRINT num; " is a composite number."
END IF
FUNCTION isPrime(num)
    IF num <= 1 THEN
        isPrime = 0
        EXIT FUNCTION
    END IF
    FOR i = 2 TO num \ 2
        IF num MOD i = 0 THEN
            isPrime = 0
            EXIT FUNCTION
        END IF
    NEXT i
    isPrime = 1
END FUNCTION

37. Write a program to define a function which returns number of factors of the given number and use this function to check whether input integer is prime or composite.

Ans:

CLS
INPUT "Enter a number: ", num
PRINT "Number of factors: "; countFactors(num)
IF countFactors(num) = 2 THEN
    PRINT num; " is a prime number."
ELSE
    PRINT num; " is a composite number."
END IF
FUNCTION countFactors(num)
    count = 0
    FOR i = 1 TO num
        IF num MOD i = 0 THEN
            count = count + 1
        END IF
    NEXT i
    countFactors = count
END FUNCTION

38.Write a function to check whether input number is perfect number or not.

Ans:

CLS
INPUT "Enter a number: ", num
IF isPerfect(num) THEN
    PRINT num; " is a perfect number."
ELSE
    PRINT num; " is not a perfect number."
END IF
FUNCTION isPerfect(num)
    sum = 0
    FOR i = 1 TO num \ 2
        IF num MOD i = 0 THEN
            sum = sum + i
        END IF
    NEXT i
    isPerfect = (sum = num)
END FUNCTION

39. Write a program to calculate and display HCF of any two numbers input by the user in the main module using a function procedure.

Ans:

CLS
INPUT "Enter first number: ", num1
INPUT "Enter second number: ", num2
PRINT "HCF of "; num1; " and "; num2; " is "; HCF(num1, num2)
FUNCTION HCF(num1, num2)
    IF num2 = 0 THEN
        HCF = num1
    ELSE
        HCF = HCF(num2, num1 MOD num2)
    END IF
END FUNCTION

40. Write a program that calculate area of rectangle using a sub procedure and the perimeter of the rectangle using a function procedure.

Ans:

CLS
INPUT "Enter length of rectangle: ", length
INPUT "Enter width of rectangle: ", width
CALL calculateArea(length, width)
PRINT "Perimeter of the rectangle: "; calculatePerimeter(length, width)
SUB calculateArea(length, width)
    area = length * width
    PRINT "Area of the rectangle: "; area
END SUB
FUNCTION calculatePerimeter(length, width)
    perimeter = 2 * (length + width)
    calculatePerimeter = perimeter
END FUNCTION

41. Write a program that calculates area of circle using a function procedure and the circumference of the circle using a sub procedure.

Ans:

DECLARE FUNCTION CalculateArea (radius AS SINGLE) AS SINGLE
SUB CalculateCircumference (radius AS SINGLE)
    PRINT "Enter the radius of the circle: ";
    INPUT radius
    PRINT "Circumference of the circle: "; 2 * 3.14 * radius
END SUB

FUNCTION CalculateArea (radius AS SINGLE) AS SINGLE
    CalculateArea = 3.14 * radius * radius
END FUNCTION

DIM radius AS SINGLE
CalculateCircumference(radius)
PRINT "Area of the circle: "; CalculateArea(radius)

42. Write a program that asks a user to input length, breadth and height of a room. This program displays area of floor using a sub procedure and area of four wall using a function procedure.

Ans:

DECLARE SUB CalculateFloorArea (length AS SINGLE, breadth AS SINGLE)
FUNCTION CalculateWallArea (length AS SINGLE, breadth AS SINGLE, height AS SINGLE) AS SINGLE
    CalculateWallArea = 2 * height * (length + breadth)
END FUNCTION

DIM length AS SINGLE, breadth AS SINGLE, height AS SINGLE
PRINT "Enter the length, breadth, and height of the room: "
INPUT length, breadth, height

CalculateFloorArea(length, breadth)
PRINT "Area of the floor: "; length * breadth
PRINT "Area of four walls: "; CalculateWallArea(length, breadth, height)

SUB CalculateFloorArea (length AS SINGLE, breadth AS SINGLE)
    PRINT "Area of the floor: "; length * breadth
END SUB

43. Write a program that accepts three numbers in the main module and displays the greatest number using a sub procedure and the smallest number using a function procedure.

Ans:

DECLARE SUB DisplayGreatestNumber (num1 AS SINGLE, num2 AS SINGLE, num3 AS SINGLE)
FUNCTION FindSmallestNumber (num1 AS SINGLE, num2 AS SINGLE, num3 AS SINGLE) AS SINGLE
    IF num1 < num2 AND num1 < num3 THEN
        FindSmallestNumber = num1
    ELSEIF num2 < num1 AND num2 < num3 THEN
        FindSmallestNumber = num2
    ELSE
        FindSmallestNumber = num3
    END IF
END FUNCTION

PRINT "Enter three numbers: "
INPUT num1, num2, num3

DisplayGreatestNumber(num1, num2, num3)
PRINT "Smallest number: "; FindSmallestNumber(num1, num2, num3)

SUB DisplayGreatestNumber (num1 AS SINGLE, num2 AS SINGLE, num3 AS SINGLE)
    IF num1 > num2 AND num1 > num3 THEN
        PRINT "Greatest number: "; num1
    ELSEIF num2 > num1 AND num2 > num3 THEN
        PRINT "Greatest number: "; num2
    ELSE
        PRINT "Greatest number: "; num3
    END IF
END SUB

44. Write a program that accepts a string and displays the reverse of the string using a user defined function and a new word after removing vowel characters by using a sub procedure.

Ans:

DECLARE FUNCTION ReverseString (str AS STRING) AS STRING
SUB RemoveVowels (str AS STRING)
    newStr = ""
    FOR i = 1 TO LEN(str)
        letter = MID(str, i, 1)
        IF INSTR("aeiouAEIOU", letter) = 0 THEN
            newStr = newStr + letter
        END IF
    NEXT i
    PRINT "String after removing vowels: "; newStr
END SUB

INPUT "Enter a string: ", str
PRINT "Reverse of the string: "; ReverseString(str)
RemoveVowels(str)

FUNCTION ReverseString (str AS STRING) AS STRING
    FOR i = LEN(str) TO 1 STEP -1
        ReverseString = ReverseString + MID(str, i, 1)
    NEXT i
END FUNCTION

45. Write the output of the following programs.

a.

DECLARE SUB Series()
CLS
CALL Series
END
SUB Series
X = 1
Y = 1
FOR Z = 1 TO 4
PRINT X;
Y = Y+1
X= X*10 + Y
NEXT Z
END SUB

Output:

b.

DECLARE FUNCTION TEST (X,Y)
CLS
LET A= 5
LET B = 6
T= TEST(A,B)
PRINT T
PRINT A,B
END 
FUNCTION TEST (X,Y)
FOR P = X TO Y
X= X+Y
Y= X+Y
NEXT P
TEST= X+Y
END FUNCTION
DECLARE SUM MIN(A,B)
A= 5
B=10
CALL MIN(A,B)
END
SUB MIN (A,B)
IF A<B THEN
PRINT A
C= A+5
PRINT C
ELSE
PRINT B^2
END IF 
END SUB

Output:

46. Rewrite the following programs correcting bugs:

a. REM to display a word by extracting characters of odd position.

CLS
DECLARE SUB WORD$ (W$, R$)
INPUT "Enter a word"; W$
CALL SUB WORD$ (W$, R$)
PRINT R$
END 
SUB WORD$ (W$, R$)
FOR P= 1 TO LEN (W$) STEP 2
E$= LEFT$ (W$, 1, P)
R$= E$ + R$
NEXT P
END SUB

Ans: Corrected program:

CLS
DECLARE SUB WORD$ (W$, R$)
INPUT "Enter a word"; W$
CALL WORD$ (W$, R$)
PRINT R$
END 

SUB WORD$ (W$, R$)
    FOR P = 1 TO LEN(W$) STEP 2
        E$ = MID$(W$, P, 1) ' Corrected LEFT$ to MID$
        R$ = R$ + E$ ' Corrected order of concatenation
    NEXT P
END SUB

b. Rem to check whether given number is odd or even using function.

DECLARE FUNCTION OE(A)
INPUT"Enter any number"; A
B$=OE$
PRINT B$
END 
FUNCTION OE$(A)
R= A MOD 2
IF R = 0 THE OE$= EVEN ELSE OE$="ODD"
STOP

Ans: corrected program:

DECLARE FUNCTION OE$(A)
INPUT "Enter any number: "; A
B$ = OE$(A)
PRINT B$
END 

FUNCTION OE$(A)
    R = A MOD 2
    IF R = 0 THEN
        OE$ = "EVEN"
    ELSE
        OE$ = "ODD"
    END IF
END FUNCTION

c. REM reverse a string

DECLARE SUB REV$(W$)
CLS
INPUT"Enter a string";W$
CALL REV$
END
SUB REV$(W$)
FOR P= 1 TO LEN$(W$)
E$= MID$(W$,P,1)
REV$=REV$+E$
NEXT P
PRINT"Reverse of a string is"; REV$
END SUB

Ans: Corrected program:

DECLARE SUB REV(W$)

CLS
INPUT "Enter a string: "; W$
CALL REV(W$)
END

SUB REV(W$)
    DIM REV$ AS STRING
    FOR P = LEN(W$) TO 1 STEP -1
        E$ = MID$(W$, P, 1)
        REV$ = REV$ + E$
    NEXT P
    PRINT "Reverse of the string is: "; REV$
END SUB

d.

DECLARE FUNCTION DIGITSUM(N)
REM displays sum of digits of a number
CLS
INPUT"Enter a number"; Num
DS= DIGITSUM(N)
PRINT"Sum of digits"; DS
END 
FUNCTION DIGITSUM(N)
FOR P = 1 TO N
R= P MOD N
DS=DS+R
N=N/10
NEXT P
DS= DIGITSUM(N)
END FUNCTION

Ans: Corrected program:

DECLARE FUNCTION DIGITSUM(N)

REM displays sum of digits of a number
CLS
INPUT "Enter a number: "; Num
DS = DIGITSUM(Num)
PRINT "Sum of digits: "; DS
END

FUNCTION DIGITSUM(N)
    DIM DS, R, P
    DS = 0
    WHILE N > 0
        R = N MOD 10
        DS = DS + R
        N = INT(N / 10)
    WEND
    DIGITSUM = DS
END FUNCTION

47. Study the following program and answer the given questions:

a.

DECLARE SUB Bigger(A,B)
CLS
INPUT"Enter first number";X
INPUT "Enter second number";Y
CALL Bigger(X,Y)
END
SUB Bigger (A,B)
IF A>B THEN
PRINT A+B
ELSE
PRINT A-B
END IF
END SUB

Questions:

i. What will be the output of the above program if X=20 and y = 40 ?

Ans: If X=20 and Y=40, the program will output 60. This is because the program first compares X and Y, and as Y is greater than X, it prints the sum of X and Y.

ii. List the argument and parameters used in the above program.

Ans:

  • Arguments: X, Y
  • Parameters: A, B

b.

DECLARE FUNCTION TEST (A, B, C)
LET X=4
LET Y=7
LET Z=12
PRINT TEST(x, Y, Z)
PRINT X, Y, Z
END
SUB TEST (A, B, C)
A=A+3
B=B+2
SUM= A+B+C/2
TEST= SUM
END SUB

i. What will be the output of the above program?

Ans:

15
4 10 12

ii. List the arguments and parameters used in the above program.

Arguments:

  • A, B, C (in the TEST function call)

Parameters:

  • A, B, C (in the TEST subroutine definition)

c.

DECLARE SUB PALIN(X$)
CLS
X$= "LIRIL"
END
SUB PALIN(X$)
FOR P= LEN (X$) TO 1 STEP - 1
R$=R$+MIDS(X$, P,1)
NEXT P
IF X$= R$ THEN
PRINT "palindrome"
ELSE
PRINT"not palindrome"
END IF 
END SUB

i. what statement should be added in the main module to invoke the sub procedure?

Ans: To invoke the PALIN subroutine in the main module, the following statement should be added:

CALL PALIN(X$)

ii. What will be the output of the program?

Ans: The output of the program will be:

palindrome

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *