Introduction
So.. Students we are here to guide you to learn QBASIC from easy to advance after this practice you can write your own program and you can understand the codes as well. So to start you should be familiar with basic concept of programming.
in programming sector there is many programming language which are used for different purposes. each and every languages are build different then others for different purposes. for example:
PYTHON language is used for Data Science/ Ai/Web application development.
JAVA is used for Mobile and desktop app development and more.
C/C++ is mainly used for hardware driver programming.
JavaScript is used for web application development.
and there is more other languages.
So There is one common between this all languages are given:
All programming languages works under given terms..
1.logics. AND, OR, NOT
2.if , Else Condition.
3. Data Types and variables
4.Arrays
5.Input and Output
6.Keywords
7.Boolen
After school in +2 you might learn about C language, In c language keywords are different from qbasic but the rule and principle are same as given topic hope you get it what I'm trying to explain..!
As I say every language works under same principal but the way of coding are different unique from each other.
We can learn Qbasic later on first of all have an eye on the given examples.. I'm gonna give you different language input and out put examples:
Python: Hello world Example:
Print("Hello World...!")
Java: Hello world Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
JavaScript: Hello world Example:
console.log("Hello, World!");
C :Language Hello world Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
C++ :Hello world Example:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
QBASIC: Hello world Example:
PRINT "Hello, World!"
Look at the given examples above, what are the differences? huge differences if you see closely..!
The way of coding use of special characters are different from each other.
That's all to understand lets began our QBASIC Tutorial:
before I start qbasic programming I wanna remine you the simple rule or technical term that:
each and every program runs up to down flow computer read code line by line.
EX:
CLS
INPUT "Enter your Name: ", n$
INPUT "Enter age Number:", a
PRINT "Your name is: " + n$
END
Note: To store string data in a variable we use $ sign ($var) if we don't the string will in stored it only stores the numerical values.
#> If you wanna skip to exam oriented material kindly scroll down.
You will get link that provide you verities of examples according to title.:
#Simple mathematical programming:
1. To find area of rectangle:
CLS
INPUT "Enter the length of the rectangle: ", length
INPUT "Enter the width of the rectangle: ", width
area = length * width
PRINT "The area of the rectangle is: "; area
END
2. To find simple interest:
CLS
INPUT "Enter the principal amount: ", principal
INPUT "Enter the interest rate: ", rate
INPUT "Enter the time period (in years): ", time
interest = (principal * rate * time) / 100
PRINT "The simple interest is: "; interest
END
3. Compound interest:
CLS
INPUT "Enter the principal amount: ", principal
INPUT "Enter the interest rate: ", rate
INPUT "Enter the time period (in years): ", time
compound_interest = principal * (1 + (rate / 100)) ^ time - principal
PRINT "The compound interest is: "; compound_interest
END
4. Total surface area of a box:
CLS
INPUT "Enter the length of the box: ", length
INPUT "Enter the width of the box: ", width
INPUT "Enter the height of the box: ", height
surface_area = 2 * (length * width + width * height + height * length)
PRINT "The total surface area of the box is: "; surface_area
END
5. To find Odd and Even numbers
CLS
INPUT "Enter a number: ", number
IF number MOD 2 = 0 THEN
PRINT number; " is an even number."
ELSE
PRINT number; " is an odd number."
END IF
END
# Else if, Else Examples:
1. Voting Age Finder:
CLS
INPUT "Enter your age: ", age
IF age >= 18 THEN
PRINT "You are eligible for voting!"
ELSE
PRINT "Sorry, you must be 18 years or older to vote."
END IF
END
2. Ticket pricing according to age:
CLS
INPUT "Enter your age: ", age
IF age < 5 THEN
PRINT "You get a free ticket!"
ELSE IF age >= 5 AND age <= 12 THEN
PRINT "Child ticket: $5"
ELSE IF age >= 13 AND age <= 18 THEN
PRINT "Student ticket: $8"
ELSE
PRINT "Adult ticket: $10"
END IF
END
3. Temperature Checking Program:
CLS
INPUT "Enter the temperature (in Celsius): ", temperature
IF temperature > 30 THEN
PRINT "It's hot outside!"
ELSE IF temperature < 10 THEN
PRINT "It's cold outside!"
ELSE
PRINT "The temperature is moderate."
END IF
END
4. Number Comparison Program:
CLS
INPUT "Enter the first number: ", num1
INPUT "Enter the second number: ", num2
IF num1 > num2 THEN
PRINT num1; " is greater than "; num2
ELSE IF num1 < num2 THEN
PRINT num1; " is less than "; num2
ELSE
PRINT "Both numbers are equal."
END IF
END
5. Positive / Negative Number finder:
CLS
INPUT "Enter a number: ", number
IF number > 0 THEN
PRINT "The number is positive."
ELSE IF number < 0 THEN
PRINT "The number is negative."
ELSE
PRINT "The number is zero."
END IF
END
# Looping Functions Example :
1. For loop Counting Number
CLS
FOR i = 1 TO 10
PRINT i
NEXT i
END
2. Multiplication Table
CLS
INPUT "Enter a number: ", number
FOR i = 1 TO 10
PRINT number; " x "; i; " = "; number * i
NEXT i
END
3. While loop Countdown number
CLS
count = 10
WHILE count > 0
PRINT count
count = count - 1
WEND
PRINT "Blastoff!"
END
4. Sum of Numbers While loop
CLS
sum = 0
number = 1
WHILE number <= 10
sum = sum + number
number = number + 1
WEND
PRINT "The sum of numbers from 1 to 10 is: "; sum
END
5. Factorial Calculation
CLS
INPUT "Enter a number: ", number
factorial = 1
count = 1
WHILE count <= number
factorial = factorial * count
count = count + 1
WEND
PRINT "The factorial of "; number; " is: "; factorial
END
# Library Functions Example :
1.Random Number Generation
CLS
RANDOMIZE TIMER
random_number = INT(RND * 100) + 1
PRINT "Random number between 1 and 100: "; random_number
END
2. String Length Calculation
CLS
text = "Hello, World!"
length = LEN(text)
PRINT "Length of the string: "; length
END
3. Absolute Value Calculation
CLS
number = -10
absolute_value = ABS(number)
PRINT "Absolute value of "; number; " is: "; absolute_value
END
4. Square Root Calculation
CLS
number = 25
square_root = SQR(number)
PRINT "Square root of "; number; " is: "; square_root
END
5. Maximum Value Calculation
CLS
number1 = 10
number2 = 15
maximum = MAX(number1, number2)
PRINT "Maximum value between "; number1; " and "; number2; " is: "; maximum
END
# Modular programming Example :
' Main Program
CLS
PRINT "Welcome to the Greeting Program!"
name = GetName()
Greet(name)
END
' Subroutine to get the user's name
FUNCTION GetName()
INPUT "Enter your name: ", name$
GetName = name$
END FUNCTION
' Subroutine to greet the user
SUB Greet(name$)
PRINT "Hello, " + name$ + "! Welcome to the program."
END SUB
# File Handling Example :
opening *.txt file using QBASIC
OPEN "example.txt" FOR OUTPUT AS #1
PRINT #1, "Hello, World!"
PRINT #1, "This is an example of file handling."
CLOSE #1
OPEN "example.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
LINE INPUT #1, line
PRINT line
WEND
CLOSE #1
_______________________________________________
REM File Handling Example in QBASIC
OPEN "data.txt" FOR OUTPUT AS #1
PRINT #1, "Hello, World!"
PRINT #1, "This is an example of file handling in QBASIC."
PRINT #1, "You can use the OPEN statement to open a file for reading or writing."
PRINT #1, "The PRINT statement can be used to write data to the file."
PRINT #1, "Finally, use the CLOSE statement to close the file."
CLOSE #1
OPEN "data.txt" FOR INPUT AS #2
PRINT "Contents of the file:"
DO UNTIL EOF(2)
LINE INPUT #2, line
PRINT line
LOOP
CLOSE #2
If you found this page is helpful then please kindly share with your friends Good Luck in Coding..!
Post a Comment