QBASIC program to input any three strings and display the shortest string among them
Introduction:
QBASIC is a simple and easy-to-use programming language that can be used to develop a wide range of applications. In this blog article, we will discuss how to write a QBASIC program to input any three strings and display the shortest string among them.
Body:
To write a QBASIC program that finds the shortest string among three strings, we can follow the following steps:
- Prompt the user to input three strings using the INPUT statement.
- Compare the length of the strings using the LEN function.
- Use a simple IF statement to compare the length of the strings and determine which string is the shortest.
- Display the shortest string to the user using the PRINT statement.
The program can be written as follows:
CLS
INPUT "Enter first string:"; a$
INPUT "Enter second string:"; b$
INPUT "Enter third string:"; c$
x = LEN(a$)
y = LEN(b$)
z = LEN(c$)
IF x < y AND x < z THEN
l$ = a$
ELSEIF y < z THEN
l$ = b$
ELSE
l$ = c$
END IF
PRINT "Smallest string="; l$
END
By using SUB procedure (SUB…END SUB)
DECLARE SUB small ()
CLS
CALL small
END
SUB small
INPUT "Enter first string:"; a$
INPUT "Enter second string:"; b$
INPUT "Enter third string:"; c$
x = LEN(a$)
y = LEN(b$)
z = LEN(c$)
IF x < y AND x < z THEN
l$ = a$
ELSEIF y < z THEN
l$ = b$
ELSE
l$ = c$
END IF
PRINT "Smallest string="; l$
END SUB
By using FUNCTION procedure (FUNCTION…END FUNCTION)
DECLARE FUNCTION longest$ (a$, b$, c$)
CLS
INPUT "Enter first string"; a$
INPUT "enter second string:"; b$
INPUT "Enter third string:"; c$
lo$ = longest$(a$, b$, c$)
PRINT "Longest string="; lo$
END
FUNCTION longest$ (a$, b$, c$)
x = LEN(a$)
y = LEN(b$)
z = LEN(c$)
IF x > y AND x > z THEN
l$ = a$
ELSEIF y > z THEN
l$ = b$
ELSE
l$ = c$
END IF
longest$ = l$
END FUNCTION
Explanation:
This program prompts the user to input three strings and then compares the length of each string using the LEN function. It then uses an IF statement to determine which string is the shortest and displays the shortest string to the user using the PRINT statement.
Output:
Conclusion:
In conclusion, writing a QBASIC program to input any three strings and display the shortest string among them is a simple and straightforward task. By following the steps outlined in this article, we can easily write a program that takes input from the user and determines which string is the shortest. QBASIC is an excellent language for beginners to learn programming concepts, and this program is a great way to practice programming skills in QBASIC. With a little bit of practice, anyone can create a program that performs this task and many other useful tasks using QBASIC.
You can also Read:
No comments:
Post a Comment