Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, November 29, 2022

QBASIC program to display the Perfect Square numbers from 1 to 50.

 QBASIC program to display the Perfect Square numbers from 1 to 50.

 


QBASIC program to display the Perfect Square numbers from 1 to 50.


Introduction


If you are a computer science student, you must be familiar with the programming language QBASIC. It is an ideal language for beginners to learn how to code. In this article, we will learn how to write a QBASIC program to display perfect square numbers from 1 to 50.


What are Perfect Square Numbers?



Before we start coding, let us first understand what perfect square numbers are. A perfect square number is an integer that is the square of another integer. For example, 1, 4, 9, 16, and 25 are perfect square numbers.


Writing the QBASIC Program



Now that we know what perfect square numbers are, let us start writing the QBASIC program to display them. Follow these steps to write the program:


Writing Program



CLS
FOR x = 1 TO 50
r = SQR(x)
r1 = INT(r)
IF r = r1 THEN
PRINT r * r;
END IF
NEXT x
END



Explanation of the program


Above QBASIC program clears the screen using the "CLS" command and then proceeds to display perfect square numbers from 1 to 50. It does this by using a FOR loop that iterates from 1 to 50.

Inside the FOR loop, the program calculates the square root of the current value of x using the "SQR" function and stores it in the "r" variable. It then checks if the square root is an integer by comparing it to the integer version of itself, which is stored in the "r1" variable.

If the square root is an integer, the program prints the square of the number using the "PRINT" statement and multiplying the square root by itself.

Finally, the program moves to the next value of x using the "NEXT" statement and ends the program using the "END" statement.


By using SUB procedure (SUB…END SUB)



DECLARE SUB psqr ()
CLS
CALL psqr
END


SUB psqr
FOR x = 1 TO 50
r = SQR(x)
r1 = INT(r)
IF r = r1 THEN
PRINT r * r;
END IF
NEXT x
END SUB


By using FUNCTION procedure (FUNCTION…END FUNCTION)



DECLARE FUNCTION psqr ()
CLS
s = psqr
END


FUNCTION psqr
FOR x = 1 TO 50
r = SQR(x)
r1 = INT(r)
IF r = r1 THEN
PRINT r * r;
END IF
NEXT x
END FUNCTION


Output





Conclusion

In this article, we learned how to write a QBASIC program to display perfect square numbers from 1 to 50. We defined the variables, used a FOR loop to iterate from 1 to 50, calculated the square of the number, and checked if the square is less than or equal to 50. We hope this article helps you understand how to write simple programs in QBASIC.

FAQs

1) What is QBASIC? 
Ans: QBASIC is a high-level programming language that is easy to learn and use. It is commonly used for educational purposes and is ideal for beginners to learn how to code.

2) What are perfect square numbers? 
Ans: perfect square number is an integer that is the square of another integer. For example, 1, 4, 9, 16, and 25 are perfect square numbers.

3) How do you write a QBASIC program to display perfect square numbers? 
Ans: To write a QBASIC program to display perfect square numbers, you need to define the variables, use a FOR loop to iterate from 1 to 50, calculate the square of the number, and check if the square is less than or equal to 50.

4) What are the benefits of learning QBASIC? 
Ans: Learning QBASIC is beneficial for beginners as it is an easy-to-learn programming language. It helps in understanding the basic concepts of programming and prepares you for more advanced programming languages.



No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages