Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Flipp Book class 9

Flipp Book class 9

Published by Memorica Graphics, 2021-03-11 12:04:10

Description: Flipp Book class 9

Search

Read the Text Version

Looping in Q-basic CHAPTER Chapter Includes 13 • Introduction of Loop • Types of Loop • FOR-NEXT, WHILE-WEND and DO-LOOP • Characteristics of different types of Loop • Nested Loop INTRODUCTION OF LOOP In our daily life, a lot of our tasks are repeated. In the same way, In the computer also, some tasks are repeated. To repeat a task in the computer, there is a looping technique. The statement which is used to repeat a task during the program execution is called loop statement. Loop statements allow to execute one or more than one statement for a number of times as long as condition is matched. Sometimes you face a situation to repeat a task many number of times. It is very tedious to write the same code repeatedly many number of times, in such situation, you can use loop statement for the best performance. Qbasic supports the following loop statements. a. FOR-NEXT b. WHILE-WEND c. DO - LOOP FOR- NEXT statement Function : Its function is to repeat given instructions specified number of times. It has a defined range of values which is controlled by the control variable and represents the number of repetition of the loop. In every iteration or loop, the counter variable receives a new value and loop will be over when it takes whole range. Syntax : FOR <counter variable> = <initial value> TO <final value> [STEP incre- ment/decrement] <Statement block> …………………….. …………………….. NEXT [same counter variable] Smart Computer Science Book-9 251

Examples: • Here, loop repeats ten numbers of times and it (i) CLS prints the given string ‘Ram Chandra’ ten times. FOR I= 1 TO 10 STEP 1 Loop will be over, when value of I is greater than PRINT “Ram Chandra” 10 because it is written in increment form. NEXT I END (ii) CLS • Here, loop repeats ten numbers of times and it FOR I= 10 TO 1 STEP -1 prints the value carrying in I variable. First time PRITN I value of I is 10, second time 9, third time 8, so on. NEXT I Loop will be over, when value of I is greater than 1 END because it is written in decrement form. Characteristics of FOR a. Loop block is started from FOR statement and closed with NEXT. b. Counter variable is always numeric type which controls repetition of the loop. c. STEP is an optional clause. Its function is to increase or decrease the initial value of the loop during the repetition of loop. d. If you have to increase the initial value of loop by 1, STEP clause is not necessary to mention, QBASIC supports this function by default. But rest of others increment or decrement we must mention. e. FOR –NEXT does not take any action in the following two situations. i) If the initial value is smaller than the final value and STEP is in decrement. ii) If the initial value is greater than the final value and STEP is in increment. f. Initial value and final value may be a variable or expression, not only value. g. Counter variable is an optional with NEXT. It is enough only NEXT. But if you are willing to apply, you must use same variable which is used with FOR. Sample Programs • By default it takes STEP+1. It is not necessary to (a) CLS mention. FOR I = 1 TO 10 • Semicolon with PRINT statement displays output PRINT I ^ 2; in same line. NEXT I END Output: 252 Looping in Q-basic

(b) CLS • Expression can be also used as an initial or final value to FOR I = 1 TO 10 / 2 control the loop. PRINT I ^ I; NEXT END Output: (c) CLS INPUT \"Enter Initial value and Final Value For LOOP:\"; X, Y FOR I = X TO Y • Variable can be used as an initial or final value to control the PRINT I; loop. NEXT • Write the output if the value of x=5 and the value of Y=10 END Output: (d) CLS • Fraction values can be used as initial or FOR I = 1.5 TO 10.5 final value to control the loop. PRINT I; NEXT I END Output: (e) CLS • Negative value can be used as the final FOR I = 1 TO -10 STEP -1 value of loop to repeat a loop backward. PRINT I; NEXT I END Output: (f) CLS • Fraction value can be used in incre- FOR I = 10 TO 1 STEP -1.5 ment or decrement values. PRINT I NEXT I END Output: (g) CLS • Comma is used with the PRINT statement to dis- play the output in five zones. FOR I = 1 TO 100 STEP +20 • + sign is an optional with increment. If it is not men- PRINT I, tioned, by default it takes +sign . NEXT END Output: Smart Computer Science Book-9 253

(h) CLS Output FOR I = 1 TO 5 PRINT I • Here, it is not using comma or semicolon in the NEXT end of PRINT statement. So, it gives the output END in vertical form. (i) CLS • The Loop does not come in action, if the initial value is greater FOR I = 5 TO 1 than the final value and decrement STEP is missing. Such PRINT I; function of loop is called, skipped loop. NEXT END Output: (j) CLS • The Loop does not come in action, if the initial value is FOR I = 1 TO 5 STEP-1 smaller than the final value and decrement STEP is PRINT 5*I; mentioned. Such function of loop is also called, skipped NEXT loop. END Output: Sample Programs 3. Write a program to ask a number and display its multiples up to 10th terms. 1. Write a program to ask your name and print 10 times. CLS INPUT \"Enter a number:\"; N CLS FOR I = 1 TO 10 INPUT \"Enter your name :\"; N$ PRINT N * I FOR I = 1 TO 10 NEXT I PRINT N$ END NEXT END 4. Write a program to ask a number and display its multiplication table up to 2. Write a program to ask 10 numbers 10th terms. and find their sum. CLS CLS INPUT \"Enter a number:\"; N FOR I = 1 TO 10 FOR I = 1 TO 10 INPUT \"Enter number:\"; N PRINT N; \"*\"; I; \"=\"; N * I S=S+N NEXT I NEXT I END PRINT \"Sum=\"; S END 5. Write a program to ask your name 254 Looping in Q-basic

and print 'n' number of times. CLS FOR I= 1 TO 19 STEP 2 CLS PRINT I; INPUT \"Enter your name :\"; N$ NEXT I INPUT \"Enter Number of times:\";N END FOR I = 1 TO N b. 10, 9, 8, 7...............1 PRINT N$ NEXT CLS END FOR I= 10 TO 1 STEP-1 PRINT I; 6. Write a program to ask 'n' numbers and NEXT I find their sum. END CLS c. 0, 5, 10, 15, 20,...........50 INPUT \"Enter total number to input:\";X FOR I = 1 TO X CLS INPUT \"Enter number:\"; N FOR I= 0 TO 50 STEP 5 S=S+N PRINT I; NEXT I NEXT I PRINT \"Sum=\"; S END END d. 1, 4, 9, 16......................100 7. Write a program to display first 10 natu- ral numbers. CLS FOR I=1 TO 10 CLS PRINT I^2; FOR I= 1 TO 10 NEXT I PRINT I END NEXT I END e. 100, 81, 64.................1 8. Write a program to find the sum of first CLS 10 natural numbers. FOR I=10 TO 1 STEP-1 PRINT I^2; CLS NEXT I FOR I= 1 TO 10 END S=S+I NEXT I f. 1, 8, 27, .......................100 PRINT \"Sum of first 10 natural numbers:\";S CLS END FOR I=1 TO 10 PRINT I^2; 9. Write a program to display following: NEXT I END a. 1, 3, 5, 7...................19 Smart Computer Science Book-9 255

1. Write the output PRINT A^I; a. CLS NEXT I END FOR I=1 TO 10 STEP 1.5 Output: PRINT I; NEXT I f. CLS END A=10 CLS FOR I= 1 TO 10 Output: PRINT A; A=A-1.5 b. CLS NEXT I FOR I= 1 TO 10/2 END S=S+I NEXT I Output: PRINT S END 2. Write a program to ask a number and display multiplication table up to 10th Output: terms. c. CLS Ans:........................................................ P=1 ................................................................ FOR I=1 TO 5 ................................................................ P=P*I; ................................................................ NEXT I ................................................................ PRINT P ................................................................ END 3. Write a program to display only even Output: numbers from 2 to 100. d. CLS Ans:........................................................ FOR I= 1 TO 5 ................................................................ PRINT I^2*2; ................................................................ NEXT I ................................................................ END ................................................................ 4. Write a program to display only odd Output: Looping in Q-basic e. CLS A=5 FOR I=1 TO 5 256

numbers from 1 to 100. 7. Write a program to find the product Ans:........................................................... of 1×2×3×..........6. .................................................................. .................................................................. Ans:...................................................... .................................................................. ............................................................. .................................................................. ............................................................. 5. Write a program to find the sum of 20 to ............................................................. ............................................................. 30. ............................................................. 8. Write a program to display: Ans:........................................................... a. 5, 25, 125.............up to 5th terms. .................................................................. .................................................................. Ans:...................................................... .................................................................. ............................................................. .................................................................. ............................................................. .................................................................. ............................................................. 6. Write a program to print your school ............................................................. b. -25, -20, 15,.........up to 20th terms. name 10 times. Ans:.......................................................... Ans:...................................................... .................................................................. ............................................................. .................................................................. ............................................................. .................................................................. ............................................................. .................................................................. ............................................................. WHILE-WEND statement Function : Its function is to repeat a series of statement or a statement mentioned in a loop block as long as the condition is true. When the condition re- turns false, then loop exits and executes next statement. Syntax : WHILE <condition> • Condition is an expression that will return non-zero <Statement block> (true)or zero (false). ………………… ………………… • Statement block is any number of statements on one or more lines which are to be executed as long as the WEND condition is true.. Smart Computer Science Book-9 257

Example : (i) REM: Repeat the LOOP forward with the increment +1 CLS • Here, initial value of loop is defined I=1 which is smaller than I=1 10 which returns the true value. So, statement mentioned be- WHILE I <= 10 tween WHILE-WNED will be executed as long as the condi- PRINT \"Ram\" tion is true. In this case, the condition is true when the value I = I + 1 of I is smaller or equal to 10. The value of I is increased by 1. WEND So, loop will be repeated 10 numbers of times. END (ii) REM: Repeat the LOOP backward with the decrement -1 CLS I = 10 WHILE I >= 1 • Here, initial value of I is defined 10 and final value is defined PRINT I 1. First time value of I =10 is greater than 1. So, condition is S = S + I true, it executes statements mentioned within a loop. Value I = I - 1 of I is decreased by 1. So, condition will return true ten times. WEND PRINT PRINT \"Sum=\"; S END Characteristics of WHILE-WEND a. Variable used in the test condition must be initialized in any place before started the WHILE. b. The body of the loop must change the value of the variable used in the tested condition, otherwise, condition will remain true in every repetition and loop will never terminate. Such loop is called an infinite loop. c. String expression can be used in the test condition. Example Programs b. REM: Calculate factorial value of 5. CLS a. REM: Display multiples of a number I=1:F=1 CLS WHILE I <= 5 I=1 F=F*I INPUT \"Enter a Number:\"; N I=I+1 WHILE I <= 10 WEND PRINT N * I PRINT \"Factorial Value:\"; F I=I+1 END WEND END 258 Looping in Q-basic

c. REM: Use of string expression in the to 10. When the value of I will be 11, test conditions test condition will return false value and CLS loop will stop. CH$ = \"Y\" WHILE CH$ = \"Y\" OR CH$ = \"y\" Sample Programs INPUT \"Enter Number:\"; N S=S+N 1. Write a program to ask your school INPUT \"Do you want to continue? name and print 10 times. [Y/N]:\"; CH$ WEND CLS PRINT \"Sum=\"; S INPUT \"Enter your school name:\"; S$ END I=1 WHILE I <= 10 d. REM: Example of Infinite loop PRINT S$ CLS I=I+1 I=1 WEND WHILE I <= 10 END PRINT \"Richa\" WEND 2. Write a program to ask 10 numbers END and find their sum. CLS • Here, value of I remains same. So, test I=1 condition always returns true and loop WHILE I <= 10 goes on, it never stop. To prevent the INPUT \"Enter number:\"; N problem of infinite loop, body of the loop S=S+N should change the value of I: In such a I=I+1 case to stop the program execution, press WEND ctrl+pause Break key simultaneously. PRINT \"Sum=\"; S END e. REM: Example of modification of Infi- nite loop 3. Write a program to display first 10 natural numbers. CLS CLS I=1 I=1 WHILE I <= 10 WHILE I <= 10 PRINT \"Richa\" PRINT I; I=I+1 I=I+1 WEND WEND END END • Here, value of I will be changed by body of 4. Write a program to display follow- the poop. In every repetition, value of I is ing: increased by 1. So, the loop will execute till the value of I is smaller than or equal a. 10, 9, 8,.............................1 Smart Computer Science Book-9 259

CLS b. CLS I = 10 N = 11111 WHILE I >= 1 I=1 PRINT I; WHILE I <= 5 I=I-1 PRINT N; WEND N = (N - 1) / 10 END I=I+1 WEND b. 2, 4, 6, 8,....................20 END CLS Output: I=2 WHILE I <= 20 c. CLS PRINT I; I=1 I=I+2 WHILE I <= 5 WEND PRINT I ^ 2 * 2; END I=I+1 WEND c. 1, 4, 7, 10, 13.....................19 END CLS Output: I=1 WHILE I <= 19 d. CLS PRINT I; I=1 I=I+3 WHILE I <= 15 WEND IF I MOD 3 = 0 THEN PRINT I; END I=I+1 WEND Test Your Skill END 1. Write the output: Output: a. CLS 2. Write a program to display multiples of 5 N=4 up to 10th terms. I=1 WHILE I <= 5 Ans:.......................................................... PRINT N; N = (N * 10) + 4 .................................................................. I=I+1 WEND ................................................................. END ................................................................. Output: 260 Looping in Q-basic

.................................................................. .............................................................. .................................................................. .............................................................. .................................................................. 5. Write a program to ask a number and find it's factorial value. 3. Write a program to display square val- ues of first 10 natural numbers. Ans:........................................................ Ans:.......................................................... ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... ................................................................. ............................................................... 4. Write a program to input numbers and 6. Write a program to display those find their product. The program should numbers divisible by 2 and 3 from 1 terminate in the user's choice. to 20. Ans:.......................................................... Ans:....................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... .................................................................. ............................................................... Trick & TIPS • To break down the infinite execution of loop, you should press Ctrl+Pause Break combination of keys from the keyboard. Smart Computer Science Book-9 261

DO-LOOP statement DO-LOOP statement is more powerful and versatile loop than WHILE-WEND loop statement. It repeats statement block mentioned within a loop WHILE or UNTIL condition is matched. The DO-LOOP statement has two structures: a. Pre-Test b. Post Test Pre-Test: This structure controls the loop in the entry point. It checks the condition in the entry point and if it returns the true result, it enters within a loop, otherwise it does not enter within a loop. At least, execution may not come once within a loop. Syntax : DO [WHILE | UNTIL]< condition> • DO WHILE -LOOP is as like Statement block as WHILE-WEND, it executes statement block as long as the ………………… condition is true. DO UNTIL- ………………… LOOP repeats a statement block as long as the until condition is LOOP Examples : satisfied. Both they check the (i) REM: Example of Pre-Test with WHILE condition condition in the loop entry point. CLS I=1 DO WHILE I <= 10 PRINT I ^ 2; I=I+1 LOOP END Output: (ii) REM: Example of Pre-Test with UNTIL condition CLS I=1 DO UNTIL I > 10 PRINT 5 * I; I = I + 1 LOOP END Output: 262 Looping in Q-basic

Post Test: This structure controls the loop in the exit point. It enters within a loop block and executes mentioned statement block and checks the condition in the exit point. If it returns the true result, it repeats a loop, otherwise it does not repeat a loop and exits from this point. At least, execution comes once within a loop. Syntax : DO • DO - LOOP WHILE repeats [Statement block statement block as long as the WHILE condition is true. ………………... DO–LOOP UNTIL repeats a ………………..] statement block as long as LOOP [WHILE | UNTIL] condition] UNTIL condition is satisfied. Both they check the condition Example : in the exit point of loop. They (i) REM: Example Of Post Test with WHILE condition execute statement block at CLS least once. I=1 DO PRINT I ^ 3; I=I+1 LOOP WHILE I <= 10 END Output: (ii) REM: Example of Post Test with UNTIL condition CLS I=1 DO PRINT 5 * I I=I+1 LOOP UNTIL I > 10 END Output: EXIT statement Function : It is a control flow statement that exits, DO...LOOP and FOR...NEXT loop. Syntax : EXIT {DO | FOR } Examples: EXIT FOR EXIT DO Smart Computer Science Book-9 263

Sample Programs Format of nested Loop a. CLS FOR I FOR I = 1 TO 10 PRINT I [Statement block IF I = 5 THEN EXIT FOR NEXT I …………………. END …………………] • Here, when the value of I=5 then execu- tion control flow comes out of loop. It does FOR J not repeat the normal loop as given 10 times. [Statement block b. CLS …………………. PRINT \"Enter negative to exit from the loop\" …………………] I=1 DO WHILE I <= 10 NEXT J INPUT \"Enter positive Num- ber:\"; N [Statement block S=S+N IF N < 0 THEN EXIT DO …………………. LOOP PRINT \"Sum=\"; S …………………] END NEXT I • Here, if you type negative number, the ex- ecution flow comes from the out of loop. It Sample Programs does not allow to repeat statement block which mentioned within the DO WHILE – a. CLS LOOP. FOR I = 1 TO 5 Nested Loop FOR J = 1 TO I A loop comes inside a loop is called nested loop. In the programming, some PRINT J; time it is necessary to use such type of loop. A nested loop contains at least one NEXT J loop. Nested loop should not cross with each other and each loop of nested must PRINT have unique variable name. NEXT I END Output ................................................... ................................................................ ................................................................ ................................................................ ................................................................ b. CLS FOR I = 5 TO 1 STEP-1 FOR J = 1 TO I PRINT “*”; 264 Looping in Q-basic

NEXT J PRINT I; PRINT NEXT I NEXT J END PRINT Output ................................................... NEXT I ................................................................ END ................................................................ Output ................................................ ................................................................ ............................................................ ................................................................ ............................................................ CLS ............................................................ ............................................................ FOR I = 1 TO 5 FOR J = 1 TO I Test Your Skill a. 5 b. 12345 c. 5 d. 1 2 3 4 5 e. 5 2345 54 1234 44 45 345 543 123 333 43 5432 12 2222 345 5 54321 1 11111 2345 1 h. 5 4 3 2 1 i. 5 j. 5 21 4321 45 45 12345 g. 321 321 345 345 4321 21 2345 2345 f. 5 4 3 2 1 54321 1 12345 5432 12345 543 m. * n. 1 2 3 54 ** 456 o. * 5 *** 789 *** **** k. 5 5 5 5 5 l. 1 1 1 1 1 ***** ***** 4444 2222 333 333 22 22 1 1 p) 9 9 9 9 9 q) 1 2 3 4 5 r) 1 1 1 1 1 s) 1 t) 1 2 3 4 5 555 67 555 1 8 9 123 123 12345 1 Smart Computer Science Book-9 265

Sample Programs INPUT \"Enter a Number:\"; N FOR I = 1 TO N / 2 1. Write a program to ask 10 numbers and find out total even numbers. R = N MOD I CLS IF R = 0 THEN S = S + I FOR I= 1 TO 10 NEXT I INPUT “Enter Number:”; N IF N = S THEN R= N MOD 2 PRINT \"It is Perfect Number\" IF R= 0 THEN E=E+1 ELSE NEXT PRINT \"It is not Perfect Number\" PRINT “Total Even Numbers=”;E END IF END END 2. Write a program to ask 10 numbers 5. Write a program to ask a number and and find out greatest number. find out it is prime or composite. CLS INPUT “Enter number:”; N CLS G=N INPUT \"Enter a number:\", N FOR I= 1 TO 9 FOR I = 1 TO N INPUT “Enter Number:”; N IF N>G THEN G=N R = N MOD I NEXT I IF R = 0 THEN C = C + 1 PRINT “Greatest Number is :”; G NEXT I END IF C = 2 THEN PRINT \"It is Prime Number.\" 3. Write a program to ask 10 numbers IF C > 2 THEN PRINT \"It is not Prime and find out smallest numbers. Number.\" CLS INPUT “Enter number:”; N 6. Write a program to ask a number and S=N display in reverse order. FOR I= 1 TO 9 INPUT “Enter Number:”, N CLS IF N<S THEN S=N INPUT “Enter a number:”;N NEXT I WHILE N>0 PRINT “Smallest Number is :”; S R=N MOD 10 END RV=RV*10+R N=N\\10 4. Write a program to ask a number and WEND find out it is perfect number or not. PRINT RV CLS END 7. Write a program to ask a number and find out it is palindrome or not. CLS 266 Looping in Q-basic

INPUT “Enter a number:”;N DO X=N R = N MOD 10 WHILE N>0 S=S+R^3 R=N MOD 10 N = N \\ 10 RV=RV*10+R N=N\\10 LOOP WHILE N >= 1 WEND IF O = S THEN NA = A + 1 IF X=RV THEN I=I+1 PRINT “It is palindrome number” LOOP WHILE I <= 10 ELSE PRINT \"Total Armstrong no=\"; A PRINT “It is non-palindrome” END END IF END 17. Write a program to display give Fibo- nacci Series : 8. Write a program to ask a number and find out total it is Armstrong number or CLS not. A=1 B=1 CLS FOR I = 1 TO 8 INPUT \"Enter Number:\"; N PRINT A; O=N:S=0 S=A+B DO A=B B=S R = N MOD 10 NEXT I S=S+R^3 END N = N \\ 10 LOOP WHILE N >= 1 18. Write a program to display factorial IF O = S THEN value of 5. PRINT \"It is Armstrong\" CLS ELSE F=1 PRINT \"Not Armstrong\" FOR I= 1 TO 5 END F=F*I NEXT I 9. Write a program to ask 10 numbers PRINT \"Factorial value of 5=\";F and find out total non-Armstrong num- END bers. 19. Write a program to find the HCF of 8 CLS and 6. I=1 DO CLS A=8 :B=6 INPUT \"Enter Number:\"; N WHILE B<> 0 O=N:S=0 Smart Computer Science Book-9 267

R= A MOD B A=B Test Your Skill B=R WEND 1. Write a program to ask 10 numbers H=A and find out total even and odd num- PRINT \"HCF=\";H bers. END 2. Write a program to ask 10 numbers and find out sum of odd and even 20. Write a program to find the LCM of 8 numbers. and 6. 3. Write a program to display only even CLS numbers from 1 to 100. A=8 : B=6 4. Write a program to ask 10 numbers P=A*B and find out product of only odd num- WHILE B<> 0 bers. R= A MOD B A=B 5. Write a program to ask 10 numbers B=R and find out smallest and greatest WEND numbers. H=A L=P/H 6. Write a program to ask 10 numbers PRINT \"LCM=\";L and find out total prime numbers. END 7. Write a program to ask 10 numbers and find out sum of prime and product of composite numbers. 21. Write a program to display prime fac- 8. Write a program to display only prime tors of 12 numbers from 1 to 50. CLS 9. Write a program to ask 10 numbers n = 12 and find out total palindrome and non- d=2 palindrome numbers. DO UNTIL n <= 1 10. Write to display only palindrome num- 10 r = n MOD d bers between 10 to 100. IF r = 0 THEN 11. Write a program to ask 10 numbers PRINT d; find out total non-perfect numbers. n=n\\d ELSE 12. Write a program to display only per- d=d+1 fect numbers between 1 to 100. GOTO 10 END IF 13. Write a program to display only Arm- LOOP strong number. END 14. Write a program to ask 10 numbers and find out total armstrong and non- 268 Looping in Q-basic

Armstrong numbers. 21. Write a program to ask a number and display in reverse order. 15. Write a program to ask 10 numbers and find the sum of perfect and non- 22. Read the following data and perform perfect numbers. the given tasks: 16. Write a program to display only all 10,8, 9, 16, 5, 12, 20, 3, 5, 4 here digits numbers. a. Write a program to display multi- 17. Write a program to ask a number and plication table of each numbers. find out its factorial value. b. Write a program to display only 18. Write a program to ask any two num- composite numbers. bers and generate the Fibonacci se- ries . c. Write a program to find out sum and product odd and even num- 19. Write a program to ask any two num- bers. bers and find out HCF value. d. Write a program to find the facto- 20. Write a program to ask any two num- rial value of each number. bers and find out LCM value. Points to Know • Looping is a programming technique which helps to repeat a task again and again for specified number of times or as long as specified the condition is matched. • The statements which are used to repeat a task for number of times are called loop statements. • QBASIC supports three types of versatile loop statements : FOR-NEXT, WHILE- WEND and DO-LOOP. • Pre-Test: structure controls the loop in the entry point. • Post Test: This structure controls the loop in the exit point. Terms to Know Looping : A process in which a list of statements executes repeatedly. Loop : Repeating a task for specified times. Flow : The order in which instructions of the program are executed Entry Point : Start instructions of a program are executed. Exit Point : End instructions of a program are executed. Execution : Running the program and getting action. Condition : Situation of a case. Pretest : Test the condition in the entry point in program. Post test : Test the condition in the exit point. Smart Computer Science Book-9 269

Worksheet Objective Questions 1 . Fill in the Blanks: a. ...................... variable is optional with NEXT. b. The statement which is used to repeat a task is called...................... statement. c. STEP is an ................... clause. d. Never ending loop is called...................loop. e. A loop lies inside a loop is called .................. loop. f. The pre-test loop structure controls the loop in the ........................ point. g. The post test loop structure controls the loop in the ........................ point. 2 . Match the following: Repetition of task a. FOR LOOP b. WHILE NEXT c. DO WEND d. LOOPING 3. State whether the following statements are true or false: a. String expression can not be used with test condition of WHILE-WEND. b. The body of loop must change the value of the variable used in the test condition. c. Variable used in test condition can be initialized in any places of program. d. Loop can be repeated forward and backward. e. Fraction value can not be used as an increment or decrement values in loop. f. There is not difference between pre-test and post test in the result. g. It is not possible to exit from WHILE-WEND with EXIT statement. 270 Looping in Q-basic

Descriptive Questions 1. Answer the following questions: a. What is looping? b. What do you mean by loop statement? c. Differentiate between pre-test and post-test condition. d. What is nested loop? e. Differentiate between WHILE and UNTIL condition. f. Differentiate between semicolon and comma with the end of PRINT statement. g. When does FOR-NEXT loop not come in action? h. What do you mean by infinite LOOP? i. When does infinite loop exist? j. How do you break down the infinite loop? Analytical Questions c. CLS N=5 1. Write the output of following pro- F=1 grams: I=1 DO a. CLS F=F*I FOR I = 1 TO 9 STEP 2 I=I+1 PRINT I; LOOP UNTIL I > N NEXT I PRINT F END END Output ................................................... Output .................................................. b. CLS d. CLS I=1 A = -25 DO UNTIL I>5 FOR I = 1 TO 10 PRINT I, I^3; PRINT A; I=I+1 A=A+5 LOOP NEXT I END END Output .................................................. Output .................................................. e. CLS ............................................................... FOR I = 5 TO 1 STEP-1 ............................................................... FOR J = 1 TO I ............................................................... ............................................................... Smart Computer Science Book-9 271

PRINT \"*\"; END IF PRINT X NEXT J END PRINT Output ................................................ 2. Debug the following programs: NEXT I a. CLS END REM : To find the sum of 1 to 10 FOR I= 1 TO 10 Output ..................................................... S=S+1 NEXT S .................................................................. PRINT \"Sum=\";S END .................................................................. Ans .................................................... ................................................................. ............................................................ ................................................................. ............................................................ f. CLS ............................................................ FOR I = 5 TO 1 STEP-1 ............................................................ FOR J = I TO 1 STEP-1 ............................................................ PRINT j; ............................................................ NEXT J ...... PRINT b. CLS NEXT I REM: Print your name 10 times. IMPUT \"Enter your Name:\";N END I=1 WHILE I<10 Output ..................................................... PRINT N$ I=I+1 .................................................................. LOOP END .................................................................. Ans: ................................................... ................................................................. ............................................................ ................................................................. ............................................................ k. CLS A=8 B=12 10 R=A MOD B IF R=0 THEN X=B ELSE A=B B=R GOTO 10 272 Looping in Q-basic

............................................................. h. Write a program to ask 10 numbers and find their sum. ............................................................. i. Write a program to display: .............................................................. 9 79 .............................................................. 579 3579 .............................................................. 13579 ................................................... Project base Work c. CLS 1. In the chart paper, write an algorithm, REM : Display square of 10 TO 5 flowchart and program codes of QBA- FOR I=10 TO 5 SIC to ask age of 25 students and find DISPLAY I the number of students in the follow- NEXT I ing age group. END Age group Numbers Ans: .................................................... <10 ? .............................................................. 10 to 15 ? .............................................................. >15 ? .............................................................. 2. In the chart paper, write an algorithm, .............................................................. flowchart and program codes of QBA- SIC to ask all the subject marks of to- ............................................................. tal number of students of your class and find the grade as given. 3. Write a program for the following tasks: Avg. Marks Grade a. To print word \"WELCOME\" 10 times. >90 A+ b. To display multiples of 5 up to 10th >80 A >70 B+ terms. >60 B c. To display only even numbers from 2 to >50 C+ >40 C 20. <=40 D d. To display only odd numbers from 1 to 20. e. Write a program to display following: 50, 45, 40, 35,................5. f. Write a program to display 20, 18, 16, 14, 12.................2. g. Write a program to display 100, 81, 64................1. Smart Computer Science Book-9 273

Library Functions CHAPTER 14 Chapter Includes • Introduction of Function • Types of Functions • Introduction of Library Function • Types of Library Functions • Use of different types of Library Functions INTRODUCTION OF FUNCTIONS QBASIC supports predefined or ready-made program code to perform the specific task are called functions, which take data, processes them and return a value in numeric or string type. Once a function is made, it can be used again and again in any part of the program. Every function has given a unique name that name is used to execute a function in a program called calling function. When function is called, it needs data or values that we should pass. The data or values passed into the function is called passing argument. Function can be called in two method one is with PRINT statement and another is assigning the return value in a variable. For example: PRINT LEN (“Kathmandu”) • Here, LEN is a function which is called with PRINT statement; “Kathmandu” is passed argument into this function. It displays length of passed string “Kathmandu” which is 9. S=SQR(4) • Here SQR is a function name which returns the square root of passed argument 4 and assigns in the S variable. The content of S will be 2. QBASIC supports two types of functions: 1. Library functions 2. User defined functions. User Defined functions are needed to define ourselves during the programming when we need; then only we can use. Such functions are not available to all the programs. Library Function comes automatically with QBASIC system which can be used with any program. These are also called built-in functions. The library functions should be called by programmer to use in the program. Library functions are of different types such as string, mathematical, date and time, I/O, etc. 274 Library Functions

STRING FUNCTION The function which is used to process or manipulate the string type of data is called string function. String can be a set of alphanumeric characters. LEN () Function Function : It is a string processing function which returns the number of characters in a string or the number of bytes stored by a variable. Syntax : LEN (string expression) • String expression may be string constant or variable. Example : CLS W$= “SAGARMATHA” PRINT LEN(W$) END • Here, it displays 10, which is number of characters or bytes stored in the W$. LEFT$ () Function Function : Its function is to return the specified number of characters from the left most side of the string. Syntax : LEFT$(string expression, n) • Where the string expression may be a string variable or string constant. N is an integer value or variable that represents numbers of characters extracted from the left most side of the character. Example : CLS PRINT LEFT$(“KATHMANDU”,3) END • Here, it extracts 3 characters KAT from the left hand side of passed string “KATHMNADU”. Sample Programs Output ................................................ a. CLS W$ = \"PATAN\" ............................................................. FOR I = 1 TO 5 ............................................................. PRINT LEFT$(W$, I) .............................................................. NEXT I END ............................................................. • Here, first time it extracts one character P from left hand side of X$ because value of I =1, second time, it extracts two characters PA because value of I will be 2 and so on, the loop repeats five times. Smart Computer Science Book-9 275

b. CLS Output ................................................ W$ = \"PATAN\" ............................................................. N=5 ............................................................. FOR I = 1 TO 3 PRINT LEFT$(W$, N) N=N-2 NEXT I END • Here, it extracts five characters from left hand side of the string because value of N=5. Second time it extracts three characters because value of N is decreased by 2 and so on, the loop repeats three times. RIGHT$ () Function : RIGHT$ is a string function which is used to extract the specified number of characters beginning from the rightmost character of the string. Syntax : RIGHT$(string expression, n) • Where string expression may a string variable or string constant. N is an integer value or variable in the range 0 to 32767 that represents numbers of characters extracted from the rightmost side of the character. Example : CLS PRINT RIGHT$(“KATHMANDU”,4) END • Here, it extracts 4 characters ANDU from the right hand side of passed string “KATHMNADU” Sample Programs Output ................................................ a. CLS ............................................................. W$ = \"PATAN\" FOR I = 1 TO LEN(W$) ............................................................. PRINT RIGHT$(W$, I) .............................................................. NEXT I ............................................................. END • Here, it extracts one character N from right hand side of X$ because value of I =1, second time, it extracts two characters AN because value of I will be 2 and so on, the loop repeats five times because length of string is 5 which is used as final value to control the loop. 276 Library Functions

b. CLS Output ................................................ W$ = \"PATAN\" ............................................................. FOR I = LEN(W$) TO 1 STEP-2 ............................................................. PRINT RIGHT$(W$, I) .............................................................. NEXT I ............................................................. END • Here, it extracts five characters PATAN from the right hand side of the string because first time value of I=5 which is the length string. Second time it extracts three character TAN because value of I is decreased by 2 and so on. MID$ () Function Function : MID$() is used to extract specified number of characters from a string. It is more powerful than LEFT$ () and RIGHT$ () because it can per- form the task which is performed by LEFT$() and RIGHT$(). Syntax : MID$(string expression, start, n) • Where string expression may be a string variable or string constant. Start rep- resents the position measured from the leftmost character of passed string. n represents number of characters to extract. Example : PRINT MID$(“KATHMANDU”,1,3) ‘ it extracts KAT from the first position three characters. PRINT MID$(“KATHMANDU”,4,3) ‘ it extracts HMA from the 4th position three characters. PRINT MID$(“KATHMANDU”,7,3) ‘ it extracts NDU from the 7th position three characters. Sample Programs Output ................................................ a. CLS ............................................................. W$ = \"PATAN\" ............................................................. SP = 3 N=1 .............................................................. FOR I = 1 TO 3 ............................................................. PRINT MID$(W$, SP, N) SP = SP - 1 N=N+2 NEXT END • Where SP represents starting position and N represents number of characters. Smart Computer Science Book-9 277

b. CLS Output ................................................ W$ = \"PATAN\" ............................................................. SP = 1 ............................................................. N=5 .............................................................. FOR I = 1 TO 3 ............................................................. PRINT MID$(W$, SP, N) SP = SP + 1 N=N-2 NEXT END • Where SP represents starting position and N represents number of characters. Test Your Skill ................................................................ 1. Write a program to display: ................................................................ a. P ................................................................ PRO PROGR ................................................................ PROGRAM ............................................................... Ans:........................................................ c. G ................................................................ ................................................................ OGR ROGRA PROGRAM ................................................................ Ans:......................................................... ................................................................ ................................................................ ................................................................ ................................................................ ............................................................... ................................................................ ............................................................... ................................................................ b. PROGRAM ................................................................ PROGR ................................................................ ................................................................ PRO ................................................................ P Ans:........................................................ d. PROGRAM ................................................................ ROGRA ................................................................ OGR G 278 Library Functions

Ans:........................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ 2. Write a program to ask your name and print first two characters, last 2 char- ................................................................ acters and 3 characters from second e. B position. BO Ans:........................................................ BOO ................................................................ BOOK ................................................................ ................................................................ Ans:........................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ ................................................................ 3. Write a program to ask your name and ................................................................ print length, first character and last character. ................................................................ f. PROGRAM Ans:........................................................ ................................................................ PROGRA ................................................................ PROG ................................................................ PRO ................................................................ PR ................................................................ P ................................................................ ................................................................ Ans:........................................................ ............................................................... ................................................................ ............................................................... ................................................................ ................................................................ ................................................................ Smart Computer Science Book-9 279

LCASE$ () Function Function : It is a string function which is used to convert all uppercase characters in lower case. Syntax : LCASE(string expression) • String expression may be a string constant or string variable. Example : CLS W$= “KATMANDU” Output ........................................... PRINT LACASE$(W$) PRINT UCASE$(\"LALITPUR\") ......................................................... END • Here, it displays the content of W$ into lowercase. UCASE$ () Function : It is a string function which is used to convert all lowercase characters in uppercase. Syntax : LCASE (string expression) • String expression may be a string constant or string variable or string expres- sion. Example : CLS W$= “KATMANDU” Output ........................................... PRINT UACASE$(W$) PRINT UCASE$(\"LALITPUR\") ......................................................... END • Here, it displays the content of W$ into uppercase. Test Your Skill 2. Write a program to ask a word and in capital letter and print first three char- 1. Write a program to ask a word in acters and last two characters in small small letter and print 1st, 3rd and last letter. characters are in capital letter. Ans:........................................................ Ans:......................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... 280 Library Functions

Sample Programs 4. Write a program to ask your name and find out total vowels and consonants. 1. Write a program to ask a word and find out 1st character is vowel or not. 5. Write a program to ask a word and display only vowels. CLS INPUT \"Enter a Word:\"; W$ 6. Write a program to ask a word and X$ = LEFT$(W$, 1) display only consonants. X$ = UCASE$(X$) IF X$ = \"A\" OR X$ = \"E\" OR X$ = \"I\" 7. Write a program to ask your name and OR X$ = \"O\" OR X$ = \"U\" THEN find out each type of vowel. PRINT \"It is Vowel\" ELSE Sample Programs PRINT \"It is not vowel\" END IF 1. Write a program to ask a word and END display in reverse order. CLS 2. Write a program to ask a word and INPUT \"Enter a Word:\"; W$ FOR I = LEN(W$) TO 1 STEP -1 find out total number of vowels. X$ = X$ + MID$(W$, I, 1) NEXT CLS PRINT X$ INPUT \"Enter a Word:\", W$ END FOR I = 1 TO LEN(W$) X$ = MID$(W$, I, 1) 2. Write a program to ask ten words and display each word in reverse order. X$ = UCASE$(X$) CLS FOR K = 1 TO 10 IF X$ = \"A\" OR X$ = \"E\" OR X$ = INPUT \"Enter a Word:\", W$ \"I\" OR X$ = \"O\" OR X$ = \"U\" THEN FOR I = LEN(W$) TO 1 STEP -1 X$ = X$ + MID$(W$, I, 1) V=V+1 NEXT PRINT X$ END IF X$ = \"\" NEXT I NEXT END PRINT \"Total Vowels:\";V END Test Your Skill 1. Write a program to ask a alpha Test Your Skill character and find out it is vowel or not. 3. Write a program to ask a word and find out it is palindrome word or not. If 2. Write a program to ask a word and find the word can be read same from both out last character is vowel or not side that is palindrome word. Such as MAM, LIRIL, MOM, etc. 3. Write a program to ask a word and find out total number of consonants. Smart Computer Science Book-9 281

ASC () Function Function : It is a string function which is used to return the corresponding ASCII code value of a string character. Every character has own ASCII code value. Syntax : ASC(string expression ) • String expression may be a string constant or string variable. It may refer to a fixed- or variable-length string. It evaluates only first character of string, if there is more than one character. Example : CLS W$= “Biratnagar” Output ........................................... PRINT ASC(W$) PRINT ASC(\"a\") ......................................................... END • Here, it displays the ASCII code value of \"B\" and \"a\" . Test Your Skill capital letter and print each vowel with corresponding ASCII code value. 1. Write a program to ask a word and print each character with corresponding Ans:........................................................ ASCII code value. ........................................................... ........................................................... Ans:......................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... .......................................................... ........................................................... 2. Write a program to ask a word in CHR$ () Function Function : It is a string function which is used to return a character of a ASCII code value. Syntax : CHR$(number) • number is any number within the 0 to 255 which may come with numeric con- stant or variable. Example : CLS 282 Library Functions

N=65 Output ........................................... PRINT CHR$(N) ......................................................... PRINT CHR$(97) END • Here, it displays the ASCII code character of \"65\" and \"97\" . STR$ () Function Function : It is a string function which is used to convert number to string. After changing the number to string, it does not perform mathematical opera- tion. Syntax : STR$(number) • number may be constant or variable. Output ........................................... Example : CLS ......................................................... A=20 B=10 A$=STR$(A) B$=STR$(B) PRINT A$+B$ END • Here, value of A and B will be changed into the string then it can't be used for mathematical operation. VAL () Function Function : It is a string processing function which is used to change sting number to number. After changing it can be used for mathematical operation. Syntax : STR$(string number) • string number may be constant or variable. Example : CLS Output ........................................... A$=\"20\" B$=\"10\" ......................................................... PRINT A$+B$ A=VAL(A$) B=VAL(B$) PRINT A+B END • Here, values of A$ and B$ are string number that will be changed into the number, then it can be used in any mathematical operation. Smart Computer Science Book-9 283

LTRIM$ () Function Function : It is a string processing function which is used to remove leading spaces from the left hand side of the string. Syntax : LTRIM$(string) • String may be string constant or string variable. Example : CLS Output ........................................... W$ = \" NEPAL\" PRINT LEN(W$) ......................................................... W$ = LTRIM$(W$) PRINT LEN(W$) END • Here, two spaces are given in the beginning in the string that is removed using LTRIM$() function. Test Your Skill Ans:........................................................ ........................................................... 1. Write the output: Output ........................................................... ........................................................... CLS ........................................................... N=456 ........................................................... N$=STR$(N) ........................................................... PRINT LEN(N$) ........................................................... N$=LTRIM$(N$) ........................................................... PRINT LEN(N$) END 2. Write a program to ask a number and find the sum of first and last digits. RTRIM$ () Function Function : It is a string processing function which is used to remove leading spaces from the right hand side of the string. Syntax : RTRIM$(string) • String may be string constant or string variable. Example : CLS W$ = \"Kathmandu \" PRINT LEN(W$) W$ = RTRIM$(W$) Output ........................................... PRINT LEN(W$) ......................................................... 284 Library Functions

END • Here, two spaces are given in the end of string that is removed using RTRIM$() function . STRING$ () Function Function : It is a string processing function which is used to return particular character in the specified number of times. Syntax : STRING$(string) • String may be string constant or string variable. Example : CLS Output ........................................... PRINT STRING$(10, 35) ......................................................... W$=STRING$(10, 42) PRINT W$ ......................................................... PRINT STRING$(10, \"*\") END • Here, it prints 10 times character of 35 , 42 and star \"*\". Check and write output. Test Your Skill Ans:........................................................ ........................................................... 1. Write a program to ask a number and ........................................................... print its character 20 times STRING$() ........................................................... function. INSTR () Function Function : It is a string processing function which is used to search and return the position of the second string in the first string. Syntax : INSTR(start, string1, strin2) • String may be string constant or string variable. Start represents the position start to search. If it is not defined, it takes 1 automatically. Example : CLS PRINT INSTR(1, \"PROGRAMMING IS FUN\", \"FUN\") X$=\"RAJA RAMAN DEVKOTA\" Y$=\"RAMAN\" Output ........................................... PRINT INSTR(X$,Y$) ......................................................... END • Here, it prints 16 and 6 as the output .Check and write output Smart Computer Science Book-9 285

SPACE$ () Function Function : It is a string processing function which is used to generate a string of space of a specified number of character length. Syntax : SPACE$(n) • n is the number of space/s which may be numeric constant or variable. Example : CLS Output ........................................... FOR i = 1 TO 5 ......................................................... ......................................................... S$ = SPACE$(i) ......................................................... PRINT S$; I NEXT END ......................................................... • Here, it generates and return the number of space in the basis of value of I . Test Your Skill ........................................................... ........................................................... 1. Write a program to display 5 to 1 in left ........................................................... diagonal. ........................................................... Ans:........................................................ ........................................................... OCT$ () Function Function : It is a string processing function which is used to return a equivalent octal value of passed decimal value. Syntax : OCT$(n) • n is the decimal number which may be numeric constant or variable. Example : CLS Output ........................................... PRINT OCT$(32) PRINT OCT$(37) ......................................................... END • Here, it returns the equivalent octal value of 32 and 37 decimal number. Test Your Skill 2. Write a program to display octal value of 1 to 10. 1. Write a program to ask a number and display its octal value. Ans:........................................................ Ans:........................................................ ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... 286 Library Functions

HEX$ () Function Function : It is a string processing function which is used to return an equivalent hexadecimal value of passed decimal number. Syntax : HEX$(n) • n is the decimal number which may be numeric constant or variable. Example : CLS Output ........................................... PRINT HEX$(44) ......................................................... PRINT HEX$(47) END • Here, it returns the equivalent octal value of 44 and 47 decimal number. Test Your Skill 2. Write a program to display hexadecimal value of 1 to 10. 1. Write a program to ask a number and display its hexadecimal value. Ans:........................................................ ........................................................... Ans:........................................................ ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... INKEY$ () Function Function : It is an I/O (Input/Output) function which return a one or two byte string containing a character pressed from the keyboard. It always looks for a pressed key from the keyboard. Syntax : IKEY$ Example : CLS PRINT \" Press any key to continue.....\" DO WHILE INKEY$=\"\" LOOP PRINT \"**WELCOME**\" END • Here, it waits you to press something from the keyboard. After pressing any key from the keyboard, then it executes next instruction. Test in your computer. INPUT$ () Function Function : It is used to read specified number of character from the keyboard. It is necessary to press enter key after the last character is typed. Smart Computer Science Book-9 287

Syntax : result$=INPUT$(n) [, #file number] Example : CLS PRINT \"Enter characters\" X$ = INPUT$(5) PRINT X$ END • Here, it allows five characters. As soon as you type last character, it stops typing and skip the execution in next line. Test this program in your computer. DATE$ () Function Function : It is used to return the current system date in the date string format mm-dd-yyyy. Syntax : DATE$ Example : PRINT DATE$ • Here, it prints the current system date. Test this program in computer. DATE$ statement Function : It is used to set the new date in the computer system. Syntax : DATE$=string$ string$ is must be in date format mm-dd-yyyy or mm-dd-yy or mm/dd/yyyy or mm/dd/yy Example : CLS PRINT DATE$ INPUT \"Enter new date [mm-dd-yyyy]:\"; D$ DATE$ = D$ PRINT \"New Date\" PRINT DATE$ • Here, it prints the changed system date. Test this program in computer. TIME$ () Function Function : It is used to return the current system time in the date string format hh- hh:mm:ss . Syntax : TIME$ Example : PRINT TIME$ • Here, it prints the current system time. Test this program in computer. TIME$ statement Function : It is used to set the new time in the computer system. 288 Library Functions

Syntax : TIME$=string$ string$ is must be in time format hh or hh:mm or hh:mm:ss Example : CLS PRINT TIME$ INPUT \"Enter new date [hh:mm:ss]:\"; T$ TIME$ = T$ PRINT \"New Time\" PRINT TIME$ • Here, it prints the changed system time. Note: TIME$ statement and Date$ statement may not support in MAC or Linux version of QBASIC. MATHEMATICAL FUNCTION The function which is used to process or manipulate the mathematical operation is called mathematical function. Some of the common mathematical functions are: SQR () Function Function : The SQR is a mathematical function that returns the square root of any positive number. Syntax : SQR(n) • Where, n is a positive number or numeric expression. Example : CLS Output ........................................... PRINT \"Value\", \"Square Root\" ......................................................... FOR I = 1 TO 5 ......................................................... ......................................................... READ N ......................................................... SR = SQR(N) PRINT N, SR NEXT I DATA 4, 9, 16, 25, 100 END Test Your Skill 2. Write a program to display square root of only even numbers from 1 to 10. 1. Write a program to display square root value of 1 to 10. Ans:........................................................ Ans:........................................................ ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... Smart Computer Science Book-9 289

SGN () Function Function : The SGN is a mathematical function that returns sign of a number, the number may negative, positive or zero. Syntax : SGN(n) • Where, n is a numerical value. If n is positive, the SGN function returns +1. If n is zero, the SGN function returns 0. If n is negative, the SGN function returns -1. Example : CLS Output ........................................... FOR I = 1 TO 5 ......................................................... READ N S = SGN(N) PRINT N, S NEXT I DATA 10,-15, -20, 0, 50 END Test Your Skill ........................................................... ........................................................... 1. Write a program to ask any 10 numbers ........................................................... and find out total input negative and ........................................................... positive numbers. ........................................................... ........................................................... Ans:........................................................ ........................................................... ........................................................... ............................................................ ........................................................... ........................................................... ABS () Function Function : The ABS() is a mathematical function which is used to return the absolute value of a number. Absolute value is the value without its sign. Syntax : ABS(n) • Where, n is numerical value, which can be constant, variable or expression. Example : A = 40 B = 20 PRINT \"Before using ABS()\" Output ........................................... PRINT (B - A) ......................................................... PRINT \"After using ABS()\" PRINT ABS(B- A) END • Here, it displays the absolute value obtained from B-A . 290 Library Functions

Test Your Skill PRINT X PRINT Y 1. Write the output : DATA 10,-5,20,-20,10 FOR I = 1 TO 5 END READ N Output ........................................... X=X+N N = ABS(N) ......................................................... Y=Y+N NEXT I INT () Function Function : The INT() is a mathematical function which is used to return the integer value of a numerical value. INT() always rounded the value down. Syntax : INT(n) • Where, n is numerical value, which can be constant, variable or expression. Example : CLS Output ........................................... FOR I = 1 TO 5 ......................................................... ......................................................... READ N ......................................................... PRINT N, INT(N) ......................................................... NEXT I DATA -10.5,-20.65,5.9,-5.3,4.5 END • Here, it displays the corresponding integers value of given list of data. Test Your Skill Output ........................................... ......................................................... 1. Write the output : ......................................................... CLS ......................................................... FOR I = 1 TO 5 ......................................................... READ N SR = SQR(N) IF SR = INT(SR) THEN S = S+N NEXT I PRINT S DATA 10,16,9,4,6 END Smart Computer Science Book-9 291

CINT () Function Function : The CINT() is a mathematical function which is used to return the nearest rounded value of a fraction number. Syntax : INT(n) • Where, n is numerical value, which can be constant, variable or expression. Example : CLS Output ........................................... FOR I = 1 TO 5 ......................................................... READ N PRINT N, CINT(N) ......................................................... NEXT I ......................................................... DATA -10.5,-20.65,5.9,-5.3,4.5 END ......................................................... • Here, it displays the corresponding nearest rounded value of given list of data. Test Your Skill ........................................................... ........................................................... 1. Write a program to ask five subject ........................................................... marks and find out average marks. ........................................................... The average marks should be nearest ........................................................... rounded. ........................................................... Ans:........................................................ ........................................................... COS () Function Function : The COS() is a mathematical function which is used to return the co- sine of an angle. Syntax : COS(n) • Where, n is the angle that must be in radians whose cosine value is to be calculated. Radians=degree*pi/180. Example : CLS A=90 R=A*3.1416/180 C=COS(R) PRINT \"Cosine value=\";C END • Here, it displays the cosine value 90 degree of an angle. 292 Library Functions

SIN () Function Function : The SIN() is a mathematical function which is used to return the sine value of an angle. Syntax : SINE(n) • Where, n is the angle that must be mentioned in radians whose sine value is to be calculated. Radians=degree*pi/180. Example : CLS A=90 R=A*3.1416/180 S=SIN(R) PRINT \"Sine value=\";S END • Here, it displays the sine value 90 degree of an angle. TAN () Function Function : The TAN() is a mathematical function which is used to return the tangent value of an angle. Syntax : SINE(n) • Where, n is the angle that must be mentioned in radians whose sine value is to be calculated. Radians=degree*pi/180. Example : CLS A=90 R=A*3.1416/180 S=SIN(R) PRINT \"Sine value=\";S END • Here, it displays the sine value 90 degree of an angle. Test Your Skill ........................................................... ........................................................... 1. Write a program to ask five subject marks and find out average marks. ........................................................... The average marks should be nearest ........................................................... rounded. ........................................................... ........................................................... Ans:........................................................ ........................................................... ........................................................... ........................................................... Smart Computer Science Book-9 293

PRINT CONTROL FUNCTION AND STATEMENT TAB () Function Function : The TAB ( ) function moves the cursor to the specified column. It always measures from the first column of the line. Syntax : INT(n) • Where, n is numerical value, which can be constant, variable or expression. Example : CLS PRINT TAB(10);\"RAM\" PRINT TAB(15);\"Krishna\" PRINT TAB(5); \"Shah\" PRINT TAB(1); \"Janakpur\" END • Here, it starts to print the information from specified column number. Test Your Skill Output c. CLS Output FOR I=5 to 1 a. CLS PRINT tab(I);I FOR I=1 to 5 NEXT I PRINT tab(I);I END NEXT I END b. CLS Output d. CLS Output W$=\"COMPUTER\" FOR I=5 to 1 FOR I=1 to 5 PRINT tab(I);\"*\" X$= MID$(W$, I, 1) NEXT I PRINT tab(5);X$ END NEXT I END SPC () Function Function : The SPC( ) function leaves specified number of blank spaces. It counts number of spaces from the current cursor position. It always follows the semicolon. Syntax : SPC(n) 294 Library Functions

• Where, n is numerical value, which can be constant, variable or expression. Example : CLS PRINT SPC(10);\"RAM\" PRINT SPC(15);\"HANUMAN\" END • Here, it starts to print the information from followed specified blank spaces . Test Your Skill Output c. CLS Output FOR I=5 to 1 a. CLS PRINT SPC(I);I FOR I=1 to 5 NEXT I PRINT SPC(I);I END NEXT I END b. CLS Output d. CLS Output W$=\"COMPUTER\" FOR I=5 to 1 FOR I=1 to 5 PRINT SPC(I);\"*\" X$= MID$(W$, I, 1) NEXT I PRINT SPC(5);X$ END NEXT I END e. CLS Output PRINT SPC(8);\"C\" PRINT TAB(8);\"C\" PRINT SPC(4);\"O\" PRINT TAB(4;\"O\" PRINT SPC(6);\"M\" PRINT TAB(6);\"M\" END LOCATE Statement Function : The LOCATE statement is used to specify the position of row and column. Syntax : LOCATE(r, c) • Where, r is row and c is column which can be numeric constant, variable or expression. Smart Computer Science Book-9 295

Example : CLS Output LOCATE 4,5 PRINT \"QBASIC\" LOCATE 2, 6 PRINT \"NEPAL\" LOCATE 6, 2 PRINT \"PATAN\" END • Here, it starts to print the information from specified coordinate of row and column number. Test Your Skill 1. Write the output: a. R = 6: C = 5 FOR I = 1 TO 9 a. CLS Output Output R = 5: C = 5 LOCATE R, C PRINT \"*\" FOR I = 1 TO 5 IF I < 5 THEN LOCATE R, C R = R - 1: C=C+1 PRINT \"*\" ELSE R = R - 1 R=R+1 C=C+1 C = C + 1 END IF NEXT I NEXT I END CLS PRINT USING Statement END The PRINT USING screen formatting statement which is used to format the data in different ways. Number Formatting Using PRINT USING statement, we can format the numbers which includes, alignment, decimal point and comma formatting. Right Alignment Formatting The right alignment of numbers are aligned the numbers from right hand side, which is illustrated by following example: 296 Library Functions

Example: Output CLS REM: Illustration of the function of PRINT USING N=1 FOR I= 1 TO 5 PRINT USING “#####”;N N=N*10+1 NEXT I END • One digit of number takes one hash (#) symbol. Type this program and test the output: Formatting Decimal Point A point ( . ) is placed at the end of the number which is possible with PRINT USING statement. The following example, illustrate the formatting the decimal point. Example: Output CLS A=5 FOR I= 1 TO 5 PRINT USING “#####.##”;A A=A*10+5 NEXT I END Comma Formatting The comma is placed in any position in format setting that prints comma after each three digits from the right hand side of the given number. The following example illustrated the comma formatting. Example: Output CLS A=55555 FOR I= 1 TO 5 PRINT USING “########,#”;A A=(A-5)/10 NEXT I END String formatting The PRINT USING statement is also used to format string or part of a string. The '!' (exclamation), '&' (ampersand) and '\\ \\' (backslash) symbols are used as a format specifier for this task. Smart Computer Science Book-9 297

Format Specifier Function ! (Exclamation sign) To return only the leftmost character & (Ampersand) To return whole characters of the string \\ \\ (Black slash) To return displays specific number of characters. Number of characters equal to number of spaces plus two characters. The following example illustrates the string formatting: Example: Output ........................................... ......................................................... CLS N$ = \"Hikmat\" K$ = \"Jung\" ......................................................... S$ = \"Khadka\" ......................................................... PRINT USING \"!.!. &\"; N$; K$; S$ PRINT USING \"Mr. !.!. &\"; N$; K$; S$ ......................................................... PRINT USING \"!.\\ \\\"; N$; K$ PRINT USING \"Mr. !.\\ \\ is from Khadka family\"; N$; K$ END • Type the program and test the output: Test Your Skill ........................................................... ........................................................... 1. Write a program to ask quantity of and rate of 2. Write a program to ask your first name middle a computer set. The program should calculate name and Cast in separate variable and print the gross total, 15% discount and net total and name as Ram Gopal Maharjan, the output display these information formatting with right should be like R. G. Maharjan. alignment, decimal and comma formatting. Ans:........................................................ Ans:........................................................ ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... ........................................................... 298 Library Functions

Points to Know • QBASIC supports predefined or ready-made program code to perform the specific task is called functions • Once a function is made, it can be used again and again in any part of the program. • The data or values passed into the function is called passing argument. • Function can be called in two methods one is with PRINT statement and another is assigning the return value in a variable. • QBASIC supports two types of functions: i) Library functions and ii) User defined functions. • User Defined functions are needed to define ourself during the program- ming. • Library Functions come automatically with QBASIC system which can be used in any program as we like. Terms to Know Function : Ready-made program code which can be called as we like. Argument : Data or values passed into the function. Lowercase : Alphabets written in small letter. Uppercase : Alphabets written in capital letter. Worksheet Objective Questions 1 . Fill in the Blanks: a. ...................... is used to convert lowercase character to uppercase. b. ........................... is used to extract specified number of characters from the extremely left hand side of the string. c. ........................... is used to extract specified number of characters from the middle of the string. d. .......................... is used to find the number which is negative, positive or zero. Smart Computer Science Book-9 299

e. ...................... is used to find the length of the string. f. ........................... is used to extract specified number of characters from ex- tremely right hand side of the string. g. ...................... is used to convert uppercase character to lowercase. 2. State whether the following statements are true or false: a. SPC () is a device input/output function. b. User defined function is also called built-in function. c. Variable used in test condition can be initialized in any place of program. d. Library functions are available with QBASIC. e. SQR () is a string function. f. LEN() is a mathematical function. g. TAB() is a device input/output function. Descriptive Questions 1. Answer the following questions: a. What do you mean by function? b. What you mean by calling function? c. What do you mean by argument? d. Write the types of way to call function. e. What do you mean by User defined function? f. What do you mean by Library Function? g. Why is MID$() is more powerful than LEFT$() and RIGHT$() ? h. How do you find a number is negative, positive or zero? 300 Library Functions


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook