4. Write a program to find the remainder pen is Rs. 45 divided 20 by 3. CLS ……………………………………………… INPUT \"Enter quantity of pen:\";QP ……………………………………………… RP=45 ……………………………………………… TA=QP*RP ……………………………………………… PRINT \" Total amount=\";TA END ……………………………………………… 4. Write a program to ask two numbers and find the sum and average. ………………………................................ CLS Sample Programs INPUT \"Enter two numbers:\";A,B S=A+B 1. Write a program to ask your name, Ad- AV=S/2 dress, phone, etc. PRINT \"Sum=\";S PRINT \"Average=\";AV CLS END INPUT \"Enter your name:\";N$ INPUT \"Enter Your Address:\";A$ Test Your Skill INPUT \"Enter Your Phone:\";P PRINT \"Name:\";N$ 1. Write a program to ask name of item, PRINT \"Address:\";A$ quantity and rate. Your program PRINT \"Phone:\";P should find out total amount. END 2. Write a program to find the 20 pieces ……………………………………………… of pen whose rate is Rs. 45 each. ……………………………………………… ……………………………………………… CLS ……………………………………………… LET QP=20 ……………………………………………… LET RP=45 ………………………............................... TA=QP*RP ................................................................ PRINT \"Total amount:\";TA 2. Write a program to ask 5 subject marks END and find out total and average marks. 3. Write a program to ask quantity of pen and find the total amount. The rate of Smart Computer Science Book-6 295
………………………………………………………………………………………………… ………………………………………………………………………………………………… …………………………………………………………………........................................... .................................................................................................................................... 3. Write a program to ask a number and find the remainder divided by 2. ………………………………………………………………………………………………… ………………………………………………………………………………………………… …………………………………………………………………........................................... .................................................................................................................................... DECISION MAKING STATEMENT The process of selecting a logical choice from the available options is called decision making. Decision making is the most essential part in any activities. As we take decision in our daily activities, computer also can take the decision such as to find greater or smaller, pass or fail, hot or cold, etc. QBASIC also supports decision making technique. IF-THEN-ELSE statement Function : It allows to make comparison and take decision depending upon the condition. If the condition is true, it executes one block of statement, otherwise it executes another block of statements. Syntax : Single line IF<condition>THEN <Statement>[ELSE <Statement >] Double Line IF <condition> THEN <Statement block > [ELSE <Statement block >] END IF 296 Statements of Q-basic
Sample Programs PRINT \"Passed in Maths\" ELSE 1. Write a program to ask two numbers PRINT \"Failed in Maths\" and find out smaller number. END IF END REM : Example of single line IF Test Your Skill CLS INPUT \"Enter two numbers: \";A,B 1. Write a program to ask two numbers IF A<B THEN PRINT \"Smaller is and find out greater number. :\";A ELSE PRINT \"Small- ……………………………………………… er is :\";B END ……………………………………………… Or REM : Example of double line IF ……………………………………………… CLS INPUT \"Enter two numbers: \";A,B ……………………………………………… IF A<B THEN PRINT \"Smaller is :\";A ……………………………………………… ELSE PRINT \"Smaller is :\";B ………………………............................... END IF END ................................................................ 2. Write a program to ask marks in maths ................................................................ and find out your are passed or failed in maths. If your marks is above 40, ................................................................ your are passed otherwise failed. 2. Write a program to ask your comput- CLS er marks and find out you can study INPUT \"Enteer maks in computer not. If your marks is above Maths:\";M 80, you can study computer otherwise IF M>40 THEN PRINT \"Passed you cannot study. Maths\" ELSE PRINT \"Failed in maths\" ……………………………………………… END ……………………………………………… or ……………………………………………… CLS ……………………………………………… INPUT \"Enteer maks in Maths:\";M IF M>40 THEN ……………………………………………… Smart Computer Science Book-6 297
………………………............................... ................................................................ 5. Write a program to ask age of your ................................................................ younger brother and find out that he ................................................................ can go to school or not. If his age is above or equal to 3, he can go to ................................................................ school otherwise he cannot go to 3. Write a program to ask age of Rohan school. and Shohan and find out who is older. ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ………………………............................... ………………………................................ ................................................................ ................................................................ ................................................................ ................................................................ 6. Write a program to ask 5 subject ................................................................ 4. Write a program to ask age of your fa- marks and find out total marks and average marks and find out your re- ther and mother and find out who is sult is passed or failed. If your aver- younger. age marks is above 40, your result is passed otherwise failed. ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… ………………………............................... ……………………………………………… ................................................................ ………………………................................ ................................................................ ................................................................ 298 Statements of Q-basic
..................................................................................................................................... ........................................................... 7. Write a program to ask a number and find out it is divisible by 3 or not. ………………………………………………………………………………………………… ………………………………………………………………………………………………… …………………………………………………………………........................................... ..................................................................................................................................... ..................................................................................................................................... LOOPING Looping is an action that goes round and round until you instruct to stop it. Let's imagine, in our daily activities, how like to we repeat our task. Similarly some times,it is also necessary to repeat a task on the comput- er. So, If we have to repeat a task in the program, we use looping tech- nique. Q-BASIC supports three types of looping technique statements: i) FOR-NEXT ii) WHILE-WEND iii) DO-LOOP Here, we discuss about basic concept of FOR-NEXT FOR-NEXT statement Function : It is used to repeat a specified task for specified number of times. Syntax : FOR Counter variable = Initial value TO Final Value [Step +/-] [Statement block] NEXT [Same counter variable] Where, • Counter variable stores initial value of loop and controls the body of the loop. • STEP + function is to increase the initial value of loop by given value. • STEP - function is to decrease the initial value of loop by given value. Smart Computer Science Book-6 299
• Initial value, final value and b. CLS STEP +/- value may variable FOR I=5 to 1 STEP-1 or constant. PRINT I NEXT I • If initial value is smaller than END final value that is known as STEP + case. In this case, c. CLS loop repeats the specified FOR I=1 to 10 STEP 2 statement block as long as PRINT I; initial value is smaller than NEXT I or equal to final value. END • If you have to increase ini- Test Your Skill tial value by STEP +1, it is not compulsory to mention 1. Write the output : Output but in other cases we must mention. a. CLS • If initial value is greater than FOR I=1 TO 5 the final value that is known as STEP- Case. In this case, PRINT \"Ram\" loop repeats the specified statement block as long as NEXT I initial value is greater than END or equal to final value. b. CLS Examples: FOR I=1 TO 5 Output a. CLS PRINT I FOR I=1 TO 5 NEXT I PRINT \"RAM\" END NEXT I c. CLS FOR I=1 TO 5 END PRINT I; NEXT I END 300 Statements of Q-basic
Output :………………………………….......... i. CLS FOR I=1 TO 5 d. CLS S=S+I FOR I=1 TO 10 STEP 2 NEXT I PRINT I; PRINT S NEXT I END END Output :………………………………….......... Output :………………………………….......... j. CLS e. CLS FOR I=1 TO 5 FOR I=1 TO 50 STEP 10 PRINT 2*I; PRINT I; NEXT I NEXT I END END Output :………………………………….......... Output :………………………………….......... k. CLS f. CLS FOR I=1 TO 5 FOR I=5 TO 1 STEP -1 PRINT I^2; PRINT I; NEXT I NEXT I END END Output :………………………………….......... Output :………………………………….......... l. CLS g. CLS FOR I=1 TO 5 FOR I=10 TO 1 STEP -2 S=S+I PRINT I; NEXT I NEXT I PRINT S END END Output :………………………………….......... Output :………………………………….......... h. CLS m. CLS FOR I=1 TO 10 STEP 1.5 FOR I=1 TO 5 PRINT I; P=P*I NEXT I NEXT I END Output :………………………………….......... Smart Computer Science Book-6 301
PRINT P END END 3. Write a program to ask a number and Output :………………………………….......... display multiplication table. n. CLS CLS P=1 INPUT \"Enter number:\";N FOR I=1 TO 5 FOR I=1 TO 5 P=P*I PRINT N;\"*\";I;\"=\";N*I NEXT I NEXT I PRINT P END END Test Your Skill Output :………………………………….......... 1. Write a program to ask 5 numbers Sample Programs and find their product. 1. Write a program to ask your name and ……………………………………………… print 10 times. ……………………………………………… CLS INPUT \"Enter Your name:\";N$ ……………………………………………… FOR I=1 TO 5 PRINT N$ ……………………………………………… NEXT I END ……………………………………………… 2. Write a program to ask 5 numbers ………………………................................ and find their sum. ................................................................ CLS FOR I=1 TO 5 ................................................................ INPUT \"Enter number:\";N 2. Write a program to find the sum of 10 S=S+N NEXT I to 1. PRINT S ……………………………………………… ……………………………………………… ……………………………………………… ……………………………………………… 302 Statements of Q-basic
…………………………………………… ............................................................... …………………………………………… …………………………………………… …………………………………………… ………………………….......................... …………………………………………… .............................................................. …………................................................. 5. Write a program to display cube value ............................................................... ............................................................... of 1 to 10 3. Write a program to display: 20, 18, 16, 14…………2. …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… ……………............................................. …………………………………………… 6. Write a program to ask a number and ……………………………………............ .............................................................. display multiples up to 10th terms 4. Write a program to display: 5, 10, 15……50. …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… …………………………………………… ……………………………………............ …………………………………………… ............................................................... ……………………………………............ ............................................................... Smart Computer Science Book-6 303
Points to Know • Programming language has its own set of key words or reserved words having special meaning is called statement or command. • A statement or command given to the computer that instructs the computer to take a specific action. • Every statement has own structure to use in the program which is called syntax. • If INPUT is used with more variables, the variable must be separated by comma. • The process of selecting a logical choice from the available options is called decision making. • Looping is a action that goes round and round and round until and unless you instruct to stop it. Terms to Know Reserved word : The word which is understood by programming language. Syntax : Rules or structure to use statement or command in the program. Expression : Combination of constant, variable or both with operators which returns a single value. Decision making : Selecting a logical choice from the available options. Loop : Action to repeat a task. 304 Statements of Q-basic
Worksheet Objective Questions 1. Fill in the blanks: a. Each programming language has it's own set of .................................... b. Statement ................................ the computer. c. Every statement has own structure or rule called ................................... d. Program is made up of a series of .......................................... e. .................................. is used to clear the screen. f. ............................ statement is used to enter data from the from the key board. g. ................................ statement is used to display the output on the screen. h. ................................... statement is used to assign a value. i. ................................ statement is used to repeat a task. 2. State whether the following statements are true or false: a. We must follow the syntax of statement during the writing program. b. The variable must be separated by comma, if INPUT is used with multiple variables. c. REM is an executable statement. d. END is an non executable statement. e. LET is a compulsory statement. f. IF-THEN-ELSE allows to repeat a task. g. Looping also allows to make comparison. Smart Computer Science Book-6 305
Descriptive Questions 1. Answer the following questions: a. What is Key word? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... b. What is statement? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... c. What does statement take action? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... d. What is syntax? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... e. What do you mean by optional statement? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... f. What do you mean by non executable statement? Ans:............................................................................................................................. .................................................................................................................................... 306 Statements of Q-basic
..................................................................................................................................... g. What do you mean by decision making statement? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... .................................................................................................................................... h. Why do you use loop statement? Ans:............................................................................................................................. ..................................................................................................................................... .................................................................................................................................... .................................................................................................................................... i. What is the use of FOR-NEXT ? Ans:.............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... ..................................................................................................................................... j. Why do you use REM statement? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... k. How does IF-THEN-ELSE execute? Ans:............................................................................................................................. ..................................................................................................................................... ..................................................................................................................................... ..................................................................................................................................... Smart Computer Science Book-6 307
4. Write the Output of given programs: DUM = X X=Y a. CLS Y = DUM A = 20 B=4 D=X-Y PRINT A + B; PRINT D END PRINT A - B; PRINT A * B; PRINT A MOD B; Output :………………………………….......... END f. CLS Output :………………………………….......... A = 11 IF A < 10 THEN b. CLS A$ = \"CHILD\" X = 10 ELSE Y = 20 A$ = \"TEENAGER\" S = (X - Y) + X END IF PRINT S PRINT A$ END END Output :………………………………….......... Output :………………………………….......... g. CLS A = 19 c. CLS B=2 A = 18 R = A MOD B B=5 IF R=0 THEN R = A MOD B A$ = \"Even\" PRINT R ELSE END A$ = \"Odd\" END IF Output :………………………………….......... PRINT A$ END d. CLS X = 20 Output :………………………………….......... Y = 10 X=Y CLS Y=X A = 5000 D=X-Y IF A > 4000 THEN PRINT D C = (5 * A) / 100 END ELSE C = (4 * A) / 100 END IF Output :………………………………….......... PRINT C END e. CLS X = 20 Output :………………………………….......... Y = 10 308 Statements of Q-basic
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315