QBASIC | Modular programming grade 8, 9, 10

 

Intro

Modular programming is a software design technique that involves breaking down a program into smaller, self-contained modules. Each module focuses on a specific task or functionality, allowing developers to work on different parts of a program independently. These modules can be combined and reused to build more complex applications.

The main idea behind modular programming is to enhance code readability, maintainability, and reusability. By dividing a program into smaller modules, developers can focus on individual tasks and reduce the complexity of the overall codebase.

Benefits of Modular Programming

Modular programming offers several benefits for developers:

Code organization: By breaking down a program into modules, developers can organize their code more effectively. Each module can represent a specific feature or functionality, making it easier to locate and maintain code.

Code reusability: Modules can be reused across multiple projects, saving development time and effort. Developers can create a library of modules that can be easily integrated into different applications, improving productivity and code consistency.

Ease of debugging: With modular programming, debugging becomes more manageable. Developers can isolate and test individual modules, making it easier to identify and fix issues. This reduces the debugging time and effort required for large codebases.

Collaboration: Modular programming allows multiple developers to work on different modules simultaneously. Each developer can focus on their assigned module without interfering with others, enhancing collaboration and productivity.

QBASIC Modular programming Examples:

1: Calculate the Area of a Circle

Module: CalculateArea
Main Program:

PRINT "Enter the radius of the circle:"
INPUT radius
area = CalculateArea(radius)
PRINT "The area of the circle is: "; area

END

FUNCTION CalculateArea(radius)
    area = 3.14 * radius^2
    CalculateArea = area
END FUNCTION

2: Check if a Number is Even or Odd

Module: CheckEvenOdd
Main Program:

PRINT "Enter a number:"
INPUT number
IF CheckEvenOdd(number) THEN
    PRINT "The number is even."
ELSE
    PRINT "The number is odd."
END IF

END

FUNCTION CheckEvenOdd(number)
    IF number MOD 2 = 0 THEN
        CheckEvenOdd = -1
    ELSE
        CheckEvenOdd = 0
    END IF
END FUNCTION


3: Calculate the Factorial of a Number

Module: CalculateFactorial
Main Program:

PRINT "Enter a number:"
INPUT number
factorial = CalculateFactorial(number)
PRINT "The factorial of "; number; " is: "; factorial

END

FUNCTION CalculateFactorial(number)
    IF number = 0 THEN
        CalculateFactorial = 1
    ELSE
        CalculateFactorial = number * CalculateFactorial(number - 1)
    END IF
END FUNCTION


4: Convert Fahrenheit to Celsius

Module: ConvertTemperature
Main Program:

PRINT "Enter the temperature in Fahrenheit:"
INPUT fahrenheit
celsius = ConvertTemperature(fahrenheit)
PRINT "The temperature in Celsius is: "; celsius

END

FUNCTION ConvertTemperature(fahrenheit)
    celsius = (fahrenheit - 32) * 5 / 9
    ConvertTemperature = celsius
END FUNCTION

5: Find the Maximum of Two Numbers

Module: FindMaximum
Main Program:

PRINT "Enter the first number:"
INPUT num1
PRINT "Enter the second number:"
INPUT num2
maximum = FindMaximum(num1, num2)
PRINT "The maximum of "; num1; " and "; num2; " is: "; maximum

END

FUNCTION FindMaximum(num1, num2)
    IF num1 > num2 THEN
        FindMaximum = num1
    ELSE
        FindMaximum = num2
    END IF
END FUNCTION


6: Calculate the Sum of an Array

Module: CalculateSum
Main Program:

DIM numbers(5)
PRINT "Enter five numbers:"
FOR i = 1 TO 5
    INPUT numbers(i)
NEXT i
sum = CalculateSum(numbers)
PRINT "The sum of the numbers is: "; sum

END

FUNCTION CalculateSum(numbers())
    sum = 0
    FOR i = 1 TO 5
        sum = sum + numbers(i)
    NEXT i
    CalculateSum = sum
END FUNCTION

7: Check if a String is Palindrome

Module: CheckPalindrome
Main Program:

PRINT "Enter a string:"
INPUT str
IF CheckPalindrome(str) THEN
    PRINT "The string is a palindrome."
ELSE
    PRINT "The string is not a palindrome."
END IF

END

FUNCTION CheckPalindrome(str)
    len = LEN(str)
    FOR i = 1 TO len / 2
        IF MID$(str, i, 1) <> MID$(str, len - i + 1, 1) THEN
            CheckPalindrome = 0
            EXIT FUNCTION
        END IF
    NEXT i
    CheckPalindrome = -1
END FUNCTION

8: Calculate the Power of a Number

Module: CalculatePower
Main Program:

PRINT "Enter the base number:"
INPUT base
PRINT "Enter the exponent:"
INPUT exponent
power = CalculatePower(base, exponent)
PRINT "The result is: "; power

END

FUNCTION CalculatePower(base, exponent)
    result = base ^ exponent
    CalculatePower = result
END FUNCTION


9: Generate Fibonacci Series

Module: GenerateFibonacci
Main Program:

PRINT "Enter the number of terms:"
INPUT numTerms
PRINT "The Fibonacci series is:"
FOR i = 1 TO numTerms
    PRINT Fibonacci(i);
NEXT i

END

FUNCTION Fibonacci(n)
    IF n <= 1 THEN
        Fibonacci = n
    ELSE
        Fibonacci = Fibonacci(n - 1) + Fibonacci(n - 2)
    END IF
END FUNCTION

10: Reverse a String

Module: ReverseString
Main Program:

PRINT "Enter a string:"
INPUT str
reversedStr = ReverseString(str)
PRINT "The reversed string is: "; reversedStr

END

FUNCTION ReverseString(str)
    len = LEN(str)
    FOR i = len TO 1 STEP -1
        reversedStr = reversedStr + MID$(str, i, 1)
    NEXT i
    ReverseString = reversedStr
END FUNCTION



Post a Comment

Previous Post Next Post