TechTorch

Location:HOME > Technology > content

Technology

Write a QBasic Program to Count Multiples of 5 from User-Input Numbers: A Beginners Guide

January 05, 2025Technology3691
How to Write a QBasic Program to Count Multiples of 5 from User-Input

How to Write a QBasic Program to Count Multiples of 5 from User-Input Numbers: A Beginner's Guide

Welcome to this beginner's guide on writing a simple program in QBasic to count how many of the 10 numbers entered by the user are multiples of 5. This guide is designed for those new to programming and will walk you through the process step-by-step.

Introduction to QBasic

QBasic is an easy-to-learn programming language that was popular for teaching basic programming concepts. It is known for its simplicity and is particularly good for beginners due to its straightforward syntax. This guide will help you write a QBasic program to count how many of the 10 numbers entered by the user are multiples of 5.

Understanding the Problem

Our task is to write a program that:

Asks the user to enter 10 numbers. Counts how many of those numbers are multiples of 5. Displays the count of multiples of 5.

Creating the QBasic Program

Let's break down the process into steps:

Step 1: Declare Variables

We need to declare a variable to store the input numbers. We will also need a counter variable to keep track of the numbers that are multiples of 5.

Dim number As Integer
Dim count As Integer

Step 2: Prompt the User to Enter Numbers

We will use a loop to prompt the user to enter 10 numbers. We can use a For Loop for this purpose.

For i As Integer  1 To 10
    Print "Enter number "  i  ": "
    Input " InputBox$, number
    If Number Mod 5  0 Then
        count  count   1
    End If
Next i

In the above code, we use the For Loop to iterate 10 times, asking the user to input a number each time. We use the InputBox function to get the user's input. We then use the Mod operator to check if the number is divisible by 5 (i.e., if the remainder is 0). If it is, we increment the count variable.

Step 3: Display the Result

After the loop, we display the count of numbers that are multiples of 5.

Print "The number of multiples of 5 is: "  count

Putting It All Together

Here is the complete QBasic program:

' Declare variables
Dim number As Integer
Dim count As Integer
' Prompt the user to enter 10 numbers and count multiples of 5
For i As Integer  1 To 10
    Print "Enter number "  i  ": "
    Input " InputBox$, number
    If Number Mod 5  0 Then
        count  count   1
    End If
Next i
' Display the result
Print "The number of multiples of 5 is: "  count
End

Conclusion

This guide has walked you through the process of writing a simple QBasic program to count the number of multiples of 5 from user-entered numbers. By following the steps and understanding the code, you should now have a good grasp of how QBasic works and how to handle user input in QBasic programs.

Additional Resources

For more resources on QBasic programming and other programming topics, you can visit the following sites:

TutorialsPoint QBasic Tutorials Wikipedia QBasic Page CBU QBasic Tutorials