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

Saturday, December 3, 2022

QBASIC program to display addition, subtraction, multiplication, and average of input two numbers

 QBASIC program to display addition, subtraction, multiplication, and average of input two numbers




QBASIC is a high-level programming language that was developed by Microsoft. It is widely used for writing simple programs and is also used for teaching programming concepts to beginners. In this article, we will write a QBASIC program to display addition, subtraction, multiplication, and average of input two numbers


Introduction


QBASIC is a great tool for beginners to learn programming. With QBASIC, you can write simple programs that can perform complex operations. In this article, we will write a program to display the sum, difference, product, and average of two input numbers.
Writing the Program

To write the program, we will use QBASIC's built-in functions for arithmetic operations. Here's the program:




Explanation of Code:


The above QBASIC code is a program that takes two numbers as input from the user and then performs arithmetic operations on them to display their sum, difference, product, and average.

The program starts by clearing the screen using the CLS statement. Then, it prompts the user to enter the first number using the INPUT statement and stores it in the variable x. Similarly, it prompts the user to enter the second number using the INPUT statement and stores it in the variable y.

The program then calculates the sum of the two input numbers and stores it in the variable sum. It also calculates the difference of the two input numbers and stores it in the variable dif. It calculates the product of the two input numbers and stores it in the variable pro. Finally, it calculates the average of the two input numbers and stores it in the variable av.

The program then uses the PRINT statement to display the sum, difference, product, and average of the two input numbers on the screen. The END statement marks the end of the program.

To use the program, the user needs to enter two numbers when prompted and then the program will display the sum, difference, product, and average of those two numbers.

Overall, this program is a simple example of how QBASIC can be used to perform arithmetic operations on input numbers.



We can solve the above program by using SUB and FUNCTION procedure

By using SUB procedure (SUB..END SUB)

DECLARE SUB sum (x, y)

DECLARE SUB dif (x, y)

DECLARE SUB pro (x, y)

DECLARE SUB av (x, y)

CLS

INPUT "Enter first number:"; x

INPUT "Enter second number:"; y

CALL sum(x, y)

CALL dif(x, y)

CALL pro(x, y)

CALL av(x, y)

END

 

SUB av (x, y)

a = (x + y) / 2

PRINT "Average of two numbers="; a

END SUB

 

SUB dif (x, y)

su = x - y

PRINT "Difference of two numbers="; su

END SUB

 

SUB pro (x, y)

p = x * y

PRINT "Product of two numbers="; p

END SUB

 

SUB sum (x, y)

s = x + y

PRINT "Sum of two numbers="; s

END SUB

 

By using FUNCTION procedure (FUNCTION..END FUNCTION)

DECLARE FUNCTION sum (x, y)

DECLARE FUNCTION dif (x, y)

DECLARE FUNCTION pro (x, y)

DECLARE FUNCTION av (x, y)

CLS

INPUT "Enter first number:"; x

INPUT "Enter second number:"; y

PRINT "Sum of two numbers="; sum(x, y)

PRINT "Difference of two numbers="; dif(x, y)

PRINT "Product of two numbers="; pro(x, y)

PRINT "Average of two numbers="; av(x, y)

END

 

FUNCTION av (x, y)

av = (x + y) / 2

END FUNCTION

 

FUNCTION dif (x, y)

dif = x - y

END FUNCTION

 

FUNCTION pro (x, y)

pro = x * y

END FUNCTION

 

FUNCTION sum (x, y)

sum = x + y

END FUNCTION


Output:





Conclusion


In this article, we learned how to write a QBASIC program to display addition, subtraction, multiplication, and average of input two numbers. We also learned how to run and test the program. QBASIC is a great tool for beginners to learn programming concepts and write simple programs.


FAQs



Q1. What is QBASIC?
Ans: QBASIC is a high-level programming language developed by Microsoft.


Q2. What is the purpose of the INPUT statement in QBASIC?
Ans: The INPUT statement is used to prompt the user to enter data.


Q3. How do I run a QBASIC program?
Ans: To run a QBASIC program, save the code in a file with the extension ".BAS" and load the file in QBASIC, then, press F5.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages