TechTorch

Location:HOME > Technology > content

Technology

How to Write a Program to Display the Sum of 10 Random Numbers Using a FOR Loop in QBasic

June 04, 2025Technology2652
How to Write a Program to Display the Sum of 10 Random Numbers Using a

How to Write a Program to Display the Sum of 10 Random Numbers Using a FOR Loop in QBasic

Welcome back to your coding journey! This guide will walk you through creating a simple QBasic program that calculates the sum of 10 random numbers generated by the user. QBasic is a great stepping stone for beginners, and it's essential to understand the basics before moving on to more complex languages.

Understanding the Problem Statement

Our task is to create a program that accomplishes the following:

Generate 10 random numbers by user input. Calculate the sum of these 10 numbers. Display the sum to the user. Use a FOR loop to execute the necessary operations.

Setting Up Your Environment

Before we start coding, ensure you have a QBasic IDE (Integrated Development Environment) setup. You can use the MS-DOS environment or a modern virtual machine (VM) for QBasic.

Writing the QBasic Code

Now, let's dive into the code. Here's a step-by-step guide to write the program:

Step 1: Clear the Screen

Start by clearing the screen to make the output more readable.

CLS

Step 2: Seed the Random Number Generator

Use the RANDOMIZE TIMER statement to seed the random number generator with the system timer as the seed value. This ensures that the sequence of random numbers is different each time the program runs.

RANDOMIZE TIMER

Step 3: Define the FOR Loop

Use a FOR loop to generate 10 random numbers. Each number should be between 0 and 100 (inclusive).

REM SUM OF N RANDOM NUMBERS WITH THE HELP OF FOR LOOPINPUT "How many random numbers do you want to generate? ", NLET S  0FOR I  1 TO N    LET I  RND * 101    LET S  S   INEXT I

Step 4: Display the Sum

Finally, display the sum to the user using the PRINT statement.

PRINT "The sum of the "  N  " random numbers is: "; SEND

Complete QBasic Program

Here's the complete program:

CLSRANDOMIZE TIMERREM SUM OF N RANDOM NUMBERS WITH THE HELP OF FOR LOOPINPUT "How many random numbers do you want to generate? ", NLET S  0FOR I  1 TO N    LET I  RND * 101    LET S  S   INEXT IPRINT "The sum of the "  N  " random numbers is: "; SEND

Understanding the Code

CLS: Clears the screen. RANDOMIZE TIMER: Seeds the random number generator with the current system timer. INPUT: Prompts the user to input the number of random numbers to generate. LET: Assigns a value to a variable. RND * 101: Generates a random number between 0 and 100. FOR: Begins a loop that repeats the specified number of times. NEXT I: Ends the FOR loop. PRINT: Displays output to the user. END: Ends the program.

Example Output

When you run the program, it will prompt you to enter the number of random numbers to generate. For example:

How many random numbers do you want to generate? 10

The output will look like this:

The sum of the 10 random numbers is: 425

Exercises

Modify the program to accept the 10 numbers directly from the user instead of generating them randomly. Make the program more interactive, such as providing feedback if the user enters an invalid input. Add error handling to ensure that the program runs smoothly even if unexpected errors occur.

Conclusion

Congratulations! You've successfully created a QBasic program that generates and sums 10 random numbers. This is a basic yet powerful skill that will serve as a stepping stone to more complex programming tasks. Keep practicing, and always remember to test your programs thoroughly.

Have fun coding, and happy hacking!