Monday, 30 December 2024

grade-9:ch-20: formatted output in qbasic computer ex

 

Chapter-20: Formatted Output in QBASIC

1. Fill in the blanks:

(a) control (b) five (c) row and column (d) SPC () (e) **(double asterisks)

2. True or False

(a) False (b) True (c) False (d) False (e) True (f) False (g) True

3. Write down the purpose and syntax of the following statements and functions:

QBASIC Statements: Syntax and Purpose


a. TAB()
Syntax:

PRINT TAB(n); "Text"

Purpose:
Moves the print cursor to the specified column number n before displaying the text.

Example:

PRINT TAB(10); "Hello"

b. SPC()
Syntax:

PRINT SPC(n); "Text"

Purpose:
Inserts n spaces before the printed text.

Example:

PRINT SPC(5); "World"

c. SPACE$
Syntax:

variable = SPACE$(n)

Purpose:
Creates a string containing n blank spaces. Useful for formatting or padding text.

Example:

PRINT "Text" + SPACE$(5) + "More Text"

d. PRINT USING
Syntax:

PRINT USING formatString; value

Purpose:
Formats the output of numbers or strings in a specified pattern.

Example:

PRINT USING "###.##"; 123.456

Formats the number to show up to two decimal places (output: 123.46).

Another Example:

PRINT USING "$###.##"; 45.6

Formats to display as currency (output: $ 45.60).

4. Differentiate the following:

Differences in QBASIC Statements and Functions


a. PRINT Statement with a Comma vs. PRINT Statement with a Semicolon

Aspect With a Comma (PRINT ,) With a Semicolon (PRINT ;)
Purpose Aligns output into fixed-width columns. Prints output consecutively without spaces.
Output Format Adds a tab-like spacing between printed items. No additional spacing between printed items.
Example PRINT "A", "B", "C" PRINT "A"; "B"; "C"
Output A B C ABC


b. Difference Between TAB() and SPC() Functions

Aspect TAB() Function SPC() Function
Purpose Moves the cursor to a specific column position. Adds a specified number of blank spaces.
Parameters Takes a column number (e.g., TAB(10)). Takes the number of spaces (e.g., SPC(5)).
Effect Positions the output at a fixed column. Adds blank spaces before the text.
Example PRINT TAB(10); "Text" PRINT SPC(5); "Text"


c. Difference Between PRINT and PRINT USING

Aspect PRINT PRINT USING
Purpose Displays text, numbers, or variables as-is. Formats the output using a specified pattern.
Formatting No formatting control. Allows precise control over text/numbers.
Use Case General-purpose output. Formatting numbers, decimals, or strings.
Example PRINT 123.456 PRINT USING "###.##"; 123.456
Output 123.456 123.46

5. Find the output of the following programs:

Let's go through each of the QBASIC programs step by step and provide a dry run along with the expected output.


a. Code 1: Sum of Numbers

CLS
SUM = 0
FOR J = 1 TO 10
    READ N
    PRINT N,
    S = S + N
NEXT J
PRINT "SUM ="; S
DATA 2,4,6,8,5,7,9,10,9,18
END

Dry Run:

Iteration (J)

READ N

Printed Value (N)

S = S + N

Updated S (Sum)

1

2

2

0 + 2

2

2

4

4

2 + 4

6

3

6

6

6 + 6

12

4

8

8

12 + 8

20

5

5

5

20 + 5

25

6

7

7

25 + 7

32

7

9

9

32 + 9

41

8

10

10

41 + 10

51

9

9

9

51 + 9

60

10

18

18

60 + 18

78

  1. SUM = 0 initializes the variable SUM to 0.
  2. The program starts a loop from J = 1 to J = 10. For each iteration:
    • It reads a number N from the DATA statement.
    • It prints N on the same line using PRINT N,.
    • It adds N to S.
  3. After all iterations, the program prints the total sum using PRINT "SUM ="; S.

Data Values:

  • DATA 2, 4, 6, 8, 5, 7, 9, 10, 9, 18

Iteration Breakdown:

  • J=1, N=2, S = 2
  • J=2, N=4, S = 6
  • J=3, N=6, S = 12
  • J=4, N=8, S = 20
  • J=5, N=5, S = 25
  • J=6, N=7, S = 32
  • J=7, N=9, S = 41
  • J=8, N=10, S = 51
  • J=9, N=9, S = 60
  • J=10, N=18, S = 78

Final Output:

2 4 6 8 5 7 9 10 9 18
SUM = 78


b. Code 2: Names and Display

CLS
FOR J = 1 TO 5
    READ N$
    PRINT N$;
NEXT J
DATA ANIL, ANUP, BINOD, SUNIL, MAHINDRA
END

Dry Run:

Iteration (J)

READ N$

Printed Value (N$)

Printed Output

Description

1

ANIL

ANIL

ANIL

First name read and printed

2

ANUP

ANUP

ANIL ANUP

Second name read and printed

3

BINOD

BINOD

ANIL ANUP BINOD

Third name read and printed

4

SUNIL

SUNIL

ANIL ANUP BINOD SUNIL

Fourth name read and printed

5

MAHINDRA

MAHINDRA

ANIL ANUP BINOD SUNIL MAHINDRA

Fifth name read and printed

  1. DATA ANIL, ANUP, BINOD, SUNIL, MAHINDRA provides five strings: "ANIL", "ANUP", "BINOD", "SUNIL", "MAHINDRA".
  2. The loop runs 5 times, printing each name using PRINT N$; without new lines between them.

Iteration Breakdown:

  • J=1, N$ = "ANIL"
  • J=2, N$ = "ANUP"
  • J=3, N$ = "BINOD"
  • J=4, N$ = "SUNIL"
  • J=5, N$ = "MAHINDRA"

Final Output:

ANIL ANUP BINOD SUNIL MAHINDRA


c. Code 3: Formatted Output with Calculations

CLS
A = 1
SUM = 0
PRINT TAB(10); "FORMATTED OUTPUT"
FOR J = 3 TO 7
    LOCATE J, 12
    PRINT USING "**$#####.##"; A
    SUM = SUM + A
    A = A * 10 + 5
NEXT J
LOCATE J, 12
PRINT "=============="
LOCATE J + 1, 12: PRINT USING "**$#####.###"; SUM
END

Dry Run:

Iteration (J)

Value of A

Printed Value (A)

Updated SUM

New A (A * 10 + 5)

Printed Output

1 (J=3)

1

$ 1.00

0 + 1 = 1

1 * 10 + 5 = 15

$ 1.00

2 (J=4)

15

$ 15.00

1 + 15 = 16

15 * 10 + 5 = 155

$ 15.00

3 (J=5)

155

$ 155.00

16 + 155 = 171

155 * 10 + 5 = 1555

$ 155.00

4 (J=6)

1555

$ 1555.00

171 + 1555 = 1726

1555 * 10 + 5 = 15555

$ 1555.00

5 (J=7)

15555

$15555.00

1726 + 15555 = 17281

15555 * 10 + 5 = 155555

$15555.00

  1. A = 1 and SUM = 0 initialize the variables.
  2. The program prints the header FORMATTED OUTPUT at column 10.
  3. The loop runs from J = 3 to J = 7, printing the value of A in a formatted way, updating SUM, and updating A for the next iteration.
  4. After the loop, it prints the total sum under ==============.

Iteration Breakdown:

  • J = 3, A = 1PRINT USING "**$#####.##"; 1$ 1.00, SUM = 1, A = 15
  • J = 4, A = 15PRINT USING "**$#####.##"; 15$ 15.00, SUM = 16, A = 155
  • J = 5, A = 155PRINT USING "**$#####.##"; 155$ 155.00, SUM = 171, A = 1555
  • J = 6, A = 1555PRINT USING "**$#####.##"; 1555$ 1555.00, SUM = 1726, A = 15555
  • J = 7, A = 15555PRINT USING "**$#####.##"; 15555$ 15555.00, SUM = 17281, A = 155555

Final Output:

          FORMATTED OUTPUT
$ 1.00
$ 15.00
$ 155.00
$ 1555.00
$ 15555.00
==============
$ 17281.000


d. Code 4: Printing with Names and Salaries

CLS
PRINT TAB(10); "FORMATTED OUTPUT"
FOR J = 3 TO 5
    READ N$, S$, S
    LOCATE J, 5
    PRINT USING "MR.!. & EARNS"; N$; S$;
    PRINT USING "#######.##"; S
NEXT J
DATA Sailendra, Sharma, 75500
DATA Suman, Gurung, 65000
DATA Rahul, Gurung, 60500
END

Dry Run:

 

 

 

 

Row

Name & Surname

Salary

3

MR.!. & EARNS Sailendra Sharma

75500.00

4

MR.!. & EARNS Suman Gurung

65000.00

5

MR.!. & EARNS Rahul Gurung

60500.00

  1. TAB(10) prints the header FORMATTED OUTPUT starting from column 10.
  2. The loop reads the name, surname, and salary for three iterations, formatting and displaying the output using PRINT USING.
  3. The formatted output shows the name and surname followed by the salary.

Iteration Breakdown:

  • J = 3, N$ = "Sailendra", S$ = "Sharma", S = 75500
    • PRINT USING "MR.!. & EARNS"; N$; S$;MR. Sailendra Sharma & EARNS
    • PRINT USING "#######.##"; S75500.00
  • J = 4, N$ = "Suman", S$ = "Gurung", S = 65000
    • PRINT USING "MR.!. & EARNS"; N$; S$;MR. Suman Gurung & EARNS
    • PRINT USING "#######.##"; S65000.00
  • J = 5, N$ = "Rahul", S$ = "Gurung", S = 60500
    • PRINT USING "MR.!. & EARNS"; N$; S$;MR. Rahul Gurung & EARNS
    • PRINT USING "#######.##"; S60500.00

Final Output:

          FORMATTED OUTPUT
MR. Sailendra Sharma & EARNS 75500.00
MR. Suman Gurung & EARNS 65000.00
MR. Rahul Gurung & EARNS 60500.00


Summary of Outputs:

  1. Code 1 (Sum of Numbers):
    2 4 6 8 5 7 9 10 9 18
    SUM = 78

  2. Code 2 (Names):
    ANIL ANUP BINOD SUNIL MAHINDRA

  3. Code 3 (Formatted Output with Calculations):

    FORMATTED OUTPUT
    $ 1.00
    $ 15.00
    $ 155.00
    $ 1555.00
    $ 15555.00
    ==============
    $ 17281.000
    
  4. Code 4 (Names and Salaries):

    FORMATTED OUTPUT
    MR. Sailendra Sharma & EARNS 75500.00
    MR. Suman Gurung & EARNS 65000.00
    MR. Rahul Gurung & EARNS 60500.00
    

6. Write down the programs to print the following outputs:


a.

To display the provided data with proper alignment using the TAB() function in QBASIC, the program can be structured to print the headers and the data in aligned columns. Here's the QBASIC program:

CLS
REM Print the header with tabulation
PRINT TAB(10); "Shakti Electronics Private Limited"
PRINT
PRINT TAB(5); "Name"; TAB(20); "Address"; TAB(40); "Telephone"
PRINT "-----------------------------------------------"

REM Print the data with tabulation
PRINT TAB(5); "Anup"; TAB(20); "Kathmandu"; TAB(40); "5223001"
PRINT TAB(5); "Suman"; TAB(20); "Patan"; TAB(40); "5512657"
PRINT TAB(5); "Marie"; TAB(20); "Balaju"; TAB(40); "2234563"
PRINT TAB(5); "Hemant"; TAB(20); "Balaju"; TAB(40); "2345567"

END

Explanation of the Program:

  1. CLS: Clears the screen.
  2. PRINT TAB(10); "Shakti Electronics Private Limited": This prints the title of the company, starting from column 10.
  3. PRINT: Prints an empty line to separate the title from the data.
  4. PRINT TAB(5); "Name"; TAB(20); "Address"; TAB(40); "Telephone": This line prints the headers for the columns ("Name", "Address", "Telephone"), each separated by a tab to align them properly.
  5. PRINT "-----------------------------------------------": This prints a separator line to differentiate the header from the data.
  6. PRINT TAB(5); "Anup"; TAB(20); "Kathmandu"; TAB(40); "5223001": This prints the first data row for "Anup".
  7. The same pattern is repeated for the remaining entries ("Suman", "Marie", "Hemant").
  8. END: Marks the end of the program.

Expected Output:

          Shakti Electronics Private Limited

Name           Address              Telephone
-----------------------------------------------
Anup           Kathmandu            5223001
Suman          Patan                5512657
Marie          Balaju               2234563
Hemant         Balaju               2345567

This will ensure that the data is printed in aligned columns, making the output neatly formatted.

b.

To print the pattern with numbers such as:

$5.00
$50.00
$500.00
$5000.00
$50000.00

You can use the PRINT USING statement in QBASIC to format the output. Here's the QBASIC program:

CLS
A = 5

REM Loop to print the pattern
FOR J = 1 TO 5
    PRINT USING "**$#####.##"; A
    A = A * 10
NEXT J

END

Explanation:

  1. CLS: Clears the screen.
  2. A = 5: Initializes the variable A with the value 5.
  3. FOR J = 1 TO 5: A loop that runs 5 times to print the pattern.
  4. PRINT USING "**$#####.##"; A: The PRINT USING statement formats the output as a currency-style number with two decimal places. This will print A with the format 5.00, 50.00, 500.00, and so on.
  5. A = A * 10: After each iteration, A is multiplied by 10, so the next number is 50.00, 500.00, etc.
  6. END: Marks the end of the program.

Expected Output:

$  5.00
$ 50.00
$ 500.00
$ 5000.00
$ 50000.00

This will print the desired pattern of numbers, formatted to two decimal places, and aligned correctly.

No comments:

Post a Comment

Popular Posts