Tuesday 4 December 2018

Qbasic By Ramesh


Programs

1. program to print a text along with comments on the screen.

cls
'comments
rem printing text.
print"Hello Programmers"
end

2. Program to print your name on the screen.

cls
input"your first name";f$
input"Your last name";l$
color 2
print"My name is";" ";f$;" ";l$
end

3. Program to divide and print it's result.

cls
input"1st number";a
input"2nd number";b
divide = A / B
print"result=";divide
end

4. Example of if...then..elseif...else statements.

'this program
'shows the salary
'according
'to the post
rem I've included
rem three posts
rem only in this
rem program
cls
rem p$ variable
rem is for the post
'taking input
'from user
input"Enter post";p$
if p$="manager" then
print"You're manager"
s = 15000
print"Your salary is";s
elseif p$="doctor" then
s = 20000
print"You're a doctor"
print"Your salary=";s
elseif p$="peon" then
s = 10000
print"You're peon"
print"Your salary=";s
else
print"You're not in the list"
end if
end

5. Program to find tax of input salary.

'this
'program calculates tax
'for different posts
rem 1% for manager
rem of total salary
rem 2% for boss of
rem company
rem and 0.5% for
rem peon
cls
input"Enter post";p$
if p$="manager" then
salary = 20000 'suppose
tax = salary * (1 / 100)
elseif p$="boss" then
salary = 50000 'suppose
tax = salary * (2 / 100)
elseif p$="peon" then
salary = 10000
tax = salary * (0.5 / 100)
else
print"No post matched"
end if
print"Your salary=";salary
print"Your tax=";tax
end

6. Program related to select case statements.(a simple calculator)

'this program
'calculates the
'addition
'subtraction
'multiplication
'and division
'according to choice
'of users
cls
print"1-addition"
print"2-subtraction"
print"3-multiplication"
print"4-division"
input"Enter choice";c
input"1st number";a
input"2nd number";b
select case c
case 1
s = A + B
print"sum=";s
case 2
ss = A - B
print"result=";ss
case 3
mm = A * B
print"Product=";mm
case 4
dd = A / B
print"result=";dd
case else
print"You did a mistake"
end select
end

7. select...case to display the names of a week according to code like 1-7 for the names of 7 days of the week.

cls
input"Enter code";c
select case c
case 1
print"sunday"
case 2
print"Monday"
case 3
print"Tuesday"
case 4
print"Wednesday"
case 5
print"Thursday"
case 6
print"Friday"
case 7
print"Saturday"
case else
color 2 'green color code
beep 'beep sound
print"Error"
end select
end

8. Program to count vowels and consonants in entered word.

cls
vowels = 0
consonants = 0
input"Enter a word";word$
for initializer=len(word$) to 1 step -1
select case ucase$(mid$(word$,initializer,1))
case is="A",is="E",is="I",is="O",is="U"
vowels = vowels + 1
case else
consonants = consonants + 1
end select
next i
print"Vowels=";vowels
print"Consonants=";consonants
end

Explaination:
In the above program we saw vowels=0 and consonants=0 which means their initial values are equal to zero means they are of no value that is suited for calculation simply.We've declared this variable and defined it's value 0,it's because we don't want to make result alter due to any reason,if we supply to zero,it will be easy for us because previous defined values may not affect this value. If we write simply this program only in QBASIC IDE,it's not necessary to assign both the variable to zero but we should not do so(We've to be aware of errors and wrong output).I hope you understood why we write this(simply)If you're begineer it's sufficient(i think).

Coming to another statement,we saw input which stores the value received by user in word$ variable which is string variable,means which can store string value only(in this case we need a word which is a string,so our input is correct,right?of course yes.)

Coming to the another line,we saw for initializer=len(word$) to 1 step -1 which means loop is started.Let me clearify about library functions used in this statament.Here initializer is a variable for the loop.len(word$) counts the no of letters in the entered word including space.In this case if we enter 'demo' as an input without quotes,the len() function makes 4 as it's value in this line then the line will be executed as: 4 to 1 step -1 means loop runs in descending order.

Now another line tells to take one letter from the word that we entered as an input.It's said by mid$(word$,initializer,1).Then ucase$ converts that one letter extracted by mid$() function to uppercase means the word is converted into capital letter.

Again we saw case is="A",is="E",is="I",is="O",is="U" where we test each letter individually using 'is' and if it gets matched then we increase the value of vowels by 1 in each loop.If not matched we treat that not matched letters as consonants(of course letter which are not vowels are consonants),so we do consonants = consonants + 1 so that consonants can also be found by this method.Finally this method continues and when loop finishes to execute,we print the values of the variable stored in each individual variables(i.e. vowels and consonants) as our required output.

NOTE:WE CONVERTED EACH INDIVIDUAL LETTERS INTO CAPITAL LETTERS FOR ERROR FREE CALCULATIONS.

9. Program to print this pattern.
5
55
555
5555
55555

cls
a$ = "55555"
for i=1 to 5
print left$(a$,i)
next i
end

It's simple to understand,just see in chapter-9 about left$() then you'll understand.In this program you can replace for i=1 to 5 by for i=1 to len(a$).They both are same thing.There is another way to do this by using nested loop.

10. Program to print the following series.

11111
2222
333
44
5

cls
for i=1 to 5
for j=5 to i step -1
print i;
next j
print
next i
end


11. Program to print the following patterns

12345
1234
123
12
1

cls
for i=5 to 1 step -1
for j=1 to i
print j;
next j
print
next i
end

12. Program to print the following patterns

1
12
123
1234
12345

cls
for i=1 to 5
for j=1 to i
print j;
next j
print
next i
end

13. Program to print the following patterns.

55555
4444
333
22
1


cls
for i=5 to 1 step -1
for j=1 to i
print i;
next j
print
next i
end

14. Program to find whether entered word is palindrome or not.

cls
input"Enter a word";w$
for i=len(w$) to 1 step -1
m$ = m$ + mid$(W$, I, 1)
next i
if w$=m$ then
print"It is palindrome"
else
print"It is not palindrome"
end if
end



15. Write a program to find square of entered number.

cls
input"Enter a number";a
s=a^2
print"Square of entered number=";s
end


16. Program to find square root of the given number.

cls
input"Enter a number";n
sq=n^(1/2)
print"Square root=";sq
end


17. Program to find cube root of the entered number.

cls
input"Enter a number";n
cu=n^(1/3)
print"cube root=";cu
end


18. Program to find area of square using FUNCTION...END FUNCTION.

declare function area(l,b)
cls
input"Enter length";l
input"Enter breadth";b
print"Area=";area(l,b);" ";"square units"
end
function area(l,b)
a = L * B
AREA = a
end function


19. Program to print the following series.

20 18 16 14 ... 2

declare function series
cls
go = series
end
function series
a = 20
t = 1
do while t<=10
print a;
a = a - 2
t = t + 1
loop
end function

20. Program to print area and perimeter of circle using sub procedure(this program is made using nested call).

declare sub area
declare sub perimeter
cls
DIM SHARED r
input"Enter radius";r
call area
end

sub area
ar = (22 / 7) * R * R
rem or (22/7)*r^2
print"Area=";ar
call perimeter
end sub

sub perimeter
peri = 2 * (22 / 7) * R
print"Perimeter=";peri
end sub

Explaination:
At first two lines, we saw declarations of two functions(SUB procedure) to do our two different tasks.One is about finding area and another is about finding perimeter. in main part we saw DIM SHARED r,so what is this?? It's a keyword(reserved word) by QBASIC which makes variables global(Means the variable r is accessible in all the functions of the program,in this case it is avilable in area and perimeter function),so we can easily guess that parameter is not passed in the above program.Simply,variable r works on both functions after we declare by DIM SHARED in main part(or main module).Finally we enter value as an input and control of program goes to area function by CALL keyword used there.

Coming to another part,we saw sub area,which is our sub program that finds area of a circle. We've used a formula to find area and the result is stored in a variable ar.and we print it,which results output in the screen.Then,call perimeter again transfers program flow to perimeter function.
coming to another part,we saw sub perimeter which is function definition part(means where everything is defined for our desired calculation or any other task).Now same thing is done here and we've used formula and printed it.Again when program reaches end sub, the control flows to the area function because it was called from area function,again there it sees end sub in area function,again control flows to main function because area function was called from main function,finally in main function we see end then program ends successfully.

21. Program to create a text file and add records like name,address and telephone number in that file.

open"files.txt" for output as #1
do
cls
input"Enter name";n$
input"Enter address";add$
input"Enter telephone number";tel#
write #1,n$,add$,tel#
input"Do you want to continue";ch$
loop while ucase$(ch$)="Y"
close #1
end

NOTE:when user press y in the program,the loop runs again and the datas can be entered repetitively.Enter some more datas so that we can play more with file handling because Below programs are relates to the same file.

Program to read the datas of the same file.

open "files.txt" for input as #1
cls
while not eof(1)
input #1,n$,add$,tel#
print "NAME | Address | telephone"
print n$,add$,tel#
wend
close #1
end

Program to add the datas in the same file.

open"files.txt" for append as #1
do
cls
input"Enter name";n$
input"Enter address";add$
input"Enter telephone number";tel#
write #1,n$,add$,tel#
input"Do you want to continue";ch$
loop while ucase$(ch$)="Y"
close #1
end

Program to delete datas from the same file.

open"files.txt" for input as #1
open "temporary.txt" for output as #2
input"Enter name to delete by name";na$
cls
while not eof(1)
input #1,n$,add$,tel#
if na$<>n$ then
write #2,n$,add$,tel#
end if
wend
close #1
close #2
kill"files.txt"
name"temporary.txt" as "files.txt"
end

Explaination:
At first you may know that we created file on our hard disk and which is located at qbasic folder from where you run it.Now Let's move ahead towards the above Program. In the above program,we saw open"files.txt" for input as #1 and open"temporary.txt" for output as #2.It means the file files.txt is loaded in memory as input mode and file temporary.txt is loaded in output mode.Now we wrote there input"Enter name to delete by name";na$ which means we take input from the user and the input will be name because we're going to delete datas by entering name as an input(simply,the name which we enter will be deleted).We run loop to read each datas in the file upto end of the file.Now we wrote input #1,n$,add$,tel# so that we can read the datas from the file.Again we did
if na$<>n$ then
write #2,n$,add$,tel#
end if

Because the program do::If the name we enter and the name present inside the file is not same (if na$<>n$ then) ,then write datas in another file which is temporary.txt and is opened in buffer no.2 .Now we close both the files by close #1 and close #2 after the process of loop finishes.After that we delete "files.txt" by using kill command(it is file system command).After that we rename temporary.txt to files.txt so that we get the records deleted in this file.Actually what happened in this program is that,when the program doesn't finds the name then it writes all the datas to another file and the one found is left,and the datas are written in temporary.txt after that name"temporary.txt" as "files.txt" ,this command changes the file name so that we can again run this program and get same output.Do it yourself,You'll understand more.



No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint