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 check whether the input number is a perfect square or not

 QBASIC program to check whether the input number is a perfect square number or not


QBASIC program to check whether the input number is a perfect square number or not


Introduction:

Are you curious if your number is perfect? Do you want to verify if your input number is a perfect square or not? You've come to the right place! In this article, we'll guide you through the process of creating a QBASIC program to check whether the input number is a perfect square number or not.

What is a Perfect Square?

A perfect square is an integer that can be expressed as the product of an integer multiplied by itself. In other words, a perfect square is a square of an integer. For example, 4 is a perfect square because it can be expressed as 2*2, while 5 is not a perfect square because it cannot be expressed in this form.

Full QBASIC code



Explanation of the Code

  1. First, the code clears the screen using the CLS command.
  2. Then, the program prompts the user to enter a number using the INPUT statement and stores it in the variable 'no'.
  3. Next, the program calculates the square root of the input number using the SQR() function and assigns it to the variable 'r'.
  4. After that, the program rounds down the value of 'r' to the nearest integer using the INT() function and stores it in the variable 'r1'.
  5. The program then checks whether 'r' is equal to 'r1' using an IF statement. If the condition is true, the program prints the message 'no is perfect square number' using the PRINT statement. If the condition is false, the program prints the message 'no is not a perfect square number' using the PRINT statement.
  6. Finally, the program ends using the END statement.


We can solve above program by using FUNCTION and SUB procedure also.

By using SUB procedure (SUB…END SUB)

DECLARE SUB per (no)
CLS
INPUT "Enter any number"; no
CALL per(no)
END

SUB per (no)
r = SQR(no)
r1 = INT(r)
IF r = r1 THEN
PRINT no; "is perfect square number"
ELSE
PRINT no; "is not perfect square number"
END IF
END SUB

 

By using FUNCTION procedure (FUNCTION…END FUNCTION)

DECLARE FUNCTION per$ (no)
CLS
INPUT "Enter any number:"; no
PRINT no; per$(no)
END

FUNCTION per$ (no)
r = SQR(no)
r1 = INT(r)
IF r = r1 THEN
p$ = "is prefect square number"
ELSE
p$ = "is not perfect square number"
END IF
per$ = p$
END FUNCTION


Output:




FAQs


Q1. Can a negative number be a perfect square?
Ans: No, a negative number cannot be a perfect square because the square of any real number is always positive.

Q2. Is 0 a perfect square?
Ans: Yes, 0 is a perfect square because it can be expressed as 0^2.

Q3. Can a non-integer number be a perfect square?
Ans: No, a non-integer number cannot be a perfect square because it cannot be expressed as the product of an integer multiplied by itself.

Conclusion


In conclusion, the QBASIC program to check whether the input number is a perfect square number or not is a simple and efficient way to verify if a number is a perfect square or not. Knowing whether a number is a perfect square or not can be beneficial in various fields, including mathematics, computer science, and cryptography. QBASIC is an excellent language to learn the basics of programming and can be useful for implementing several algorithms and data structures. So, start exploring the world of perfect squares and QBASIC programming today!



No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages