Sie sind auf Seite 1von 17

CSC 102 ANSWERS TO

PRACTICAL MANUAL
QUESTIONS (2014)

OLAYINKA OKEWALE
RCF ACADEMIC UNIT - FEDERAL UNIVERSITY OF TECHNOLOGY, AKURE
ACTIVITY 3-5
QUESTION: What will be the output of this program?

REM TRUE OR FALSE

REM IN BASIC, -1 MEANS TRUE, 0 FALSE

A% = 0

B% = -1

C% = (A% AND NOT B%) OR (B% AND NOT A%)

PRINT STATEMENT C IS ; C%

END

ANSWER: The logical Expression (A% AND NOT B%) will be false, while (B% AND NOT A%) will be
true. Combining these two expressions with OR will produce a TRUE value.

OUTPUT: STATEMENT C IS -1

ACTIVITY 4-1
QUESTION: Write an interactive BASIC program that will read in a positive integer value and
determine the following:

a. If the integer is a prime number


b. If the integer is a composite number

HINT: Prime number is a natural number greater than 1 that has no positive divisors other than 1
and itself.
Composite number is a natural number greater than 1 that is not a prime number

ANSWER:

CLS
REM program to get a positive integer from the user and determine whether the integer is a
REM prime or composite number then print out the result.
10 INPUT Enter a positive number greater than 1: , N
IF (N < 0 OR N = 0) THEN GOTO 10 This would test if the number is positive
IF (N = 1) THEN
PRINT 1 is neither a prime or composite number
GOTO 10
END IF
REM to test, well get the range of numbers that can divide n without giving us an improper
REM fraction.
FOR D = 2 TO N-1
IF (N MOD D = 0) THEN
PRINT N; is a composite number
GOTO 50 End the program
END IF
NEXT D
PRINT N; is a prime number
50 END
ACTIVITY 4-2
QUESTION: Write a program that will accept a hexadecimal number as input, and display a menu
that will permit any of the following operation to be carried out

a. Display the hexadecimal equivalent of ones complement


b. Display the hexadecimal to binary
c. Display the hexadecimal to octal

HINT: Ones complement is gotten by changing the hexadecimal to binary number and inverting the
zeros to one and ones to zero then, changing back to hexadecimal

ANSWER:

CLS
REM program to display a menu and get input from the user to select the menu and process
REM a hexadecimal number accordingly.
PRINT Press 1 and <Enter> to find complement of a hexadecimal number
PRINT Press 2 and <Enter> to find binary equivalent of a hexadecimal number
PRINT Press 3 and <Enter> to find octal equivalent of a hexadecimal number
10 INPUT Menu: , menu_select
IF (menu_select <> 1 AND menu_select <> 2 AND menu_select <> 3) THEN GOTO 10
INPUT Enter a hexadecimal number > 0. Begin with &H for hexadecimal value: , hex_number
IF (menu_select = 1) THEN
CALL complement(hex_number)
ELSEIF (menu_select = 2) THEN
CALL hex_to_bin(hex_number)
ELSE
CALL hex_to_oct(hex_number)
END IF
END
SUB complement(hex_number)
REM subroutine for the complement menu
stored = hex_number
DIM remainder(16) It can only process 16 bit data. (Thats enough)
X=1
WHILE (hex_number > 0)
remainder(X) = hex_number MOD 2
hex_number = (hex_number/2) - (remainder(X) / 2)
X=X+1
WEND
REM next step is to invert the zeros to ones and vice versa.
hex_complement = 0
FOR Y = X-1 TO 1 STEP -1
IF (remainder(Y) = 1) THEN
remainder(Y) = 0
ELSE
remainder(Y) = 1
END IF
hex_complement = hex_complement + (remainder(Y) * (2 ^ (Y-1)))
NEXT Y
PRINT The complement of ; HEX$(stored); (H) is ; HEX$(hex_complement); (H)
END SUB
SUB hex_to_bin(hex_number)
REM subroutine for the hexadecimal to binary menu
stored = hex_number
DIM remainder(16) It can only process 16 bit data. (Thats enough)
X=1
WHILE (hex_number > 0)
remainder(X) = hex_number MOD 2
hex_number = (hex_number/2) - (remainder(X) / 2)
X=X+1
WEND
REM next step is to display the binary equivalent
hex_complement = 0
PRINT HEX$(stored); (H) is ;
FOR Y = X-1 TO 1 STEP -1
PRINT remainder(Y);
NEXT Y
PRINT (B)
END SUB
SUB hex_to_oct(hex_number)
REM subroutine for the hexadecimal to octal menu
REM subroutine for the hexadecimal to binary menu
stored = hex_number
DIM remainder(16) It can only process 16 bit data. (Thats enough)
X=1
WHILE (hex_number > 0)
remainder(X) = hex_number MOD 8
hex_number = (hex_number/8) - (remainder(X) / 8)
X=X+1
WEND
REM next step is to display the octal equivalent
hex_complement = 0
PRINT HEX$(stored); (H) is ;
FOR Y = X-1 TO 1 STEP -1
PRINT remainder(Y);
NEXT Y
PRINT (O)
END SUB

ACTIVITY 4-3
QUESTION: Draw a flowchart and write a program to find the sum of and average of all the multiples
of 5 between 1 and 100.

ANSWER: (Program only)


CLS
REM program to find the sum and average of all multiples of 5 between 1 and 100
SUM = 0
COUNT = 0
FOR I = 1 TO 100
IF (I MOD 5 = 0) THEN
SUM = SUM + I
COUNT = COUNT + 1
END IF
NEXT I
AVERAGE = SUM / COUNT
PRINT The sum of all multiples of 5 from 1 to 100 is ; SUM
PRINT The average of all multiples of 5 from 1 to 100 is ; AVERAGE
END

ACTIVITY 4-4
QUESTION: What is wrong with this program? CORRECTION:

REM LOGICAL AND REM LOGICAL AND


A% = -1 A% = -1
B% = 0 B% = 0
C% = A%B% C% = A% AND B%
PRINT A:B:C: PRINT A%;B%;C%;
END END
ANSWER:
On line 4, logical AND should be between A% and B%.
On line 5, A, B and C should be A%, B% and C% respectively
On line 5, colon (:) should be replaced with semi-colon (;)

ACTIVITY 4-5
QUESTION: Write out the output of this program

REM TWO PAIRS


A% = -1
B% = 0
S1% = NOT(A% AND B%)
S2% = NOT(A% OR B%)
S3% = (NOT A%) OR (NOT B%)
S4% = (NOT A%) AND (NOT B%)
PRINT A%;B%;S1%;S2%;S3%;S4%
END
ANSWER:
S1% would give -1 because (-1 AND 0) is 0 and NOT(0) is -1
S2% would give 0 because (-1 OR 0) is -1 and NOT(-1) is 0
S3% would give -1 because NOT(-1) is 0 while NOT(0) is -1 then 0 OR -1 is -1
S4% would give 0 because NOT(-1) is 0 while NOT(0) is -1 then 0 AND -1 is 0
OUTPUT: -1 0 -1 0 -1 0
ACTIVITY 4-6
QUESTION: Write a program to determine whether the Boolean expressions
X: not(A AND B)
Y: not(A or not B)
are equivalent or not
ANSWER:
CLS
REM TESTING BOOLEAN EXPRESSIONS
A = -1
B=0
X = NOT(A AND B)
Y = NOT(A OR NOT(B))
IF (X = Y) THEN
PRINT X IS EQUAL TO Y
ELSE
PRINT X IS NOT EQUAL TO Y
END IF
END

ACTIVITY 5-1
QUESTION: Write a program using loop to calculate the sum of every third integer beginning with
i=2 (i.e calculate 2+5+8+11+14) for all values of i that are less than 100.
Write the loop in each of the following ways:
i. using a FOR-NEXT structure
ii. using a DO WHILE LOOP structure
iii. using a DO UNTIL LOOP structure
ANSWER:
(i) Using a FOR-NEXT structure
CLS
REM looping program using FOR NEXT structure
SUM = 0
FOR I = 2 TO 99 STEP 3
SUM = SUM + I
NEXT I
PRINT SUM = ; SUM
END

(ii) Using a DO WHILE LOOP structure


CLS
REM looping program using DO WHILE LOOP structure
SUM = 0
I=2
DO WHILE I < 100
SUM = SUM + I
I=I+3
LOOP
PRINT SUM = ; SUM
END

(iii) Using a DO UNTIL LOOP structure


CLS
REM looping program using DO UNTIL LOOP structure
SUM = 0
I=2
DO UNTIL I = 99
SUM = SUM + I
I=I+3
LOOP
PRINT SUM = ; SUM
END

ACTIVITY 5-2
QUESTION: The following statements will produce the four possible pairings of 0 and 1 shown at the
right. Write a similar program to produce the eight triples such as 000, 001, 010

FOR A = 0 TO 1 OUTPUT:
FOR B = 0 TO 1 00
PRINT A;B 01
NEXT B 10
NEXT A 11
ANSWER:
CLS
REM program to produce the eight triples of binary.
FOR A = 0 TO 1
FOR B = 0 TO 1
FOR C = 0 TO 1
PRINT A;B;C
NEXT C
NEXT B
NEXT A
END

ACTIVITY 5-3
QUESTION: if two arrays (4, 2, 7) and (6, 3, 5) are used to represent vectors, their inner product is
defined as 4*6 + 2*3 + 7*5 = 65
Corresponding components are multiplied and the products summed. Write a program to compute
the inner product of vector having 10 components, such as (3, 5, 4, 6, 4, 3, 5, 4, 3, 5) and (4, 4, 5, 6,
7, 4, 5, 4, 3, 5).

ANSWER:
CLS
REM program to add corresponding components in two arrays
DIM X(10), Y(10)
FOR I = 1 TO 10
READ X(I)
NEXT I
DATA 3, 5, 4, 6, 4, 3, 5, 4, 3, 5
FOR I = 1 TO 10
READ Y(I)
NEXT I
DATA 4, 4, 5, 6, 7, 4, 5, 4, 3, 5
SUM = 0
FOR J = 1 TO 10
SUM = SUM + (X(J) * Y(J))
NEXT J
PRINT THE RESULT IS ; SUM
END

ACTIVITY 5-4
QUESTION: A list contains both positive and negative numbers, the mix being more or less random.
The length of the list is unknown, but from their origin, it is known that none of the numbers is as big
as 1000. This number thus been added at the end of the list where it serves as a flag. Design an
algorithm and flowchart for summing and counting the positive and negative numbers separately.
Assume that none of the numbers is zero.
ANSWER: (Algorithm only)
ALGORITHM
Find the length of the list using LEN(list_name) and store it in a variable named LIST_LEN.
Initialize POSTIVE_SUM to be zero.
Initialize NEGATIVE_SUM to be zero as well.
Initialize POSTIVE_COUNT to be zero.
Initialize NEGATIVE_COUNT to be zero as well.
Using a FOR NEXT looping structure, loop through from 1 to the LIST_LEN.
Make a conditional statement inside the loop to determine if a number is positive (by testing if it is
greater than zero) or negative by testing if it is less than zero).
Make a decision that: If the number is positive, add it to POSITIVE_SUM and assign the sum back to
the variable POSITIVE_SUM and also increment POSITIVE_COUNT by one.
Make an otherwise decision: If the number is negative, add it to NEGATIVE_SUM and assign the sum
back to the variable NEGATIVE_SUM and also increment NEGATIVE_COUNT by one.
Output the number and the sum of the positive numbers.
Output the number and the sum of the negative numbers.

ACTIVITY 5-5
QUESTION: Write a basic program to sort a given set of numbers in an ascending and descending
order.

HINT: Use any sorting algorithm of your choice


ANSWER:
CLS
REM program to sort out a given set of numbers in ascending and descending order.
LESS = 0
GREAT = 0
INPUT Enter the length of numbers you want to input: , num
DIM numbers(num)
FOR I = 1 TO num
PRINT Enter #; I
INPUT : , numbers(I)
NEXT I
REM now begin the sorting algorithm.
FOR J = 2 TO num
COUNT = J
WHILE COUNT > 0
IF (numbers(COUNT) > numbers(COUNT - 1)) THEN GOTO 2
LESS = numbers(COUNT)
GREAT = numbers(COUNT - 1)
numbers(COUNT - 1) = LESS
numbers(COUNT) = GREAT
COUNT = COUNT -1
WEND
2 NEXT J
REM print the numbers out sorted accordingly.
PRINT Numbers sorted in ascending order:
FOR K = 1 TO num
PRINT numbers(K);
NEXT K
PRINT
PRINT Numbers sorted in descending order:
FOR K = num TO 1 STEP -1
PRINT numbers(K);
NEXT K
END

ACTIVITY 5-6
QUESTION: A list of names was supposed to be in alphabetical order, but it is suspected that some
disarrangement has occurred. Develop an algorithm and flowchart to go down the list checking each
adjacent pair in turn. If a pair is found to be out of order, an error message is to be printed out.
ANSWER: (Algorithm only)
ALGORITHM
Read in the list of names and process it as an array
Looping from the second item to the last item of the array
Check if the n-th item is greater than the (n 1)-th item and continue the loop
Else, output an error message.

ACTIVITY 6-1
QUESTION: Prepare an interactive basic program to read a persons first, middle and last names and
extract the initials.
ANSWER:
CLS
REM program to print a user's name with initials from the given name
INPUT "Enter your first name: ", f_name$
INPUT "Enter your middle name: ", m_name$
INPUT "Enter your last name: ", l_name$
initials$ = LEFT$(m_name$, 1) + "." + LEFT$(f_name$, 1)
PRINT "Welcome, "; l_name$ + " "; initials$
END

ACTIVITY 7-3
QUESTION: Write a program to store the data of one hundred students in a file. The data consist of
name (maximum of 25 characters) and scores obtained in five courses during an examination.
Produce a formatted output to consist of suitable headings, serial number, students name and
scores obtained in five courses and then determine the grades obtained in each courses, total load
unit, total credit unit, and grade point average per student. Use FUTA grading system and assume
that each courses is three unit.

ANSWER:
CLS
REM program to calculate and store GPA of students
OPEN students.dat FOR OUTPUT AS #1 Open the file for writing.
FOR X = 1 TO 100
DIM score(5), grade$(5)
CONST unit = 3
INPUT Enter the name of student.: , name$
TCU = 0
TLU = 0
FOR Y = 1 TO 5
INPUT Enter the score: , score(Y)
grade$(Y) = ConvertToGrade$(score(Y))
points = ConvertGradeToPoint(grade$(Y))
TCU = TCU + (points * unit)
TLU = TLU + unit
NEXT Y
PRINT #1, ------------------------------[ STUDENT ;X; ]------------------------------ File
PRINT ------------------------------[ STUDENT ;X; ]------------------------------ Console
PRINT #1, SN: ;X; , NAME: ;name$
PRINT SN: ;X; , NAME: ;name$
PRINT #1, ----------[ SCORES AND GRADES ]---------- File
PRINT ----------[ SCORES AND GRADES ]---------- Console
FOR U = 1 TO 5
PRINT #1, SCORE #;U; = ;score(U); :: GRADE #;U; = ; grade$(U) File
PRINT SCORE #;U; = ;score(U); :: GRADE #;U; = ; grade$(U) Console
NEXT U
PRINT #1, Total Credit Unit = ; TCU File
PRINT Total Credit Unit = ; TCU Console
PRINT #1, Total Load Unit = ; TLU File
PRINT Total Load Unit = ; TLU Console
PRINT #1, Grade Point Average = ; (TCU/TLU) File
PRINT Grade Point Average = ; (TCU/TLU) Console
NEXT X
CLOSE #1 Close the file
END
FUNCTION ConvertToGrade$(score)
IF score >= 70 THEN
ConvertToGrade$ = A
ELSEIF score < 75 AND score >= 60 THEN
ConvertToGrade$ = B
ELSEIF score < 60 AND score >= 50 THEN
ConvertToGrade$ = C
ELSEIF score < 50 AND score >= 45 THEN
ConvertToGrade$ = D
ELSEIF score < 45 AND score >= 40 THEN
ConvertToGrade$ = E
ELSE
ConvertToGrade$ = F
END IF
END FUNCTION
FUNCTION ConvertGradeToPoint(grade$)
IF grade$ = A THEN
ConvertGradeToPoint = 5
ELSEIF grade$ = B THEN
ConvertGradeToPoint = 4
ELSEIF grade$ = C THEN
ConvertGradeToPoint = 3
ELSEIF grade$ = D THEN
ConvertGradeToPoint = 2
ELSEIF grade$ = E THEN
ConvertGradeToPoint = 1
ELSE
ConvertGradeToPoint = 0
END IF
END FUNCTION

ACTIVITY 8-1
QUESTION:
a. Numerologists claim to be able to determine a persons character traits based on the
numeric value of a name. The value of a name is determined by summing up the value of the
letters of the name where a is 1, b is 2, , z is 26. For example JOHN would have the
value 10+15+8+14=47. Write a program that calculate the numeric value of a single name
provided as input.
b. Expand your solution on the previous problem to allow the calculation of a complete name
such as JOHN MARVIN ZELE
ANSWER:
(a)
CLS
REM program to determine the trait of a user based on his name
DIM alphas$(26)
FOR i = 1 TO 26
READ alphas$(i)
NEXT i
DATA a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
REM get input
INPUT "Enter your name: ", a$
a$ = LCASE$(a$)
sum = 0
FOR i = 1 TO LEN(a$)
cur_letter$ = MID$(a$, i, 1)
FOR j = 1 TO 26
IF (cur_letter$ = alphas$(j)) THEN
sum = sum + j
END IF
NEXT j
NEXT i
PRINT "Your numeric value is "; sum
END

(b)
CLS
REM program to determine the trait of a user based on his name
DIM alphas$(26)
FOR i = 1 TO 26
READ alphas$(i)
NEXT i
DATA a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
REM get input
INPUT "Enter your name: ", a$
a$ = LCASE$(a$)
sum = 0
FOR i = 1 TO LEN(a$)
cur_letter$ = MID$(a$, i, 1)
IF (cur_letter$ = " ") THEN GOTO 5
FOR j = 1 TO 26
IF (cur_letter$ = alphas$(j)) THEN
sum = sum + j
END IF
NEXT j
5 NEXT i
PRINT "Your numeric value is "; sum
END

ACTIVITY 8-2
QUESTION: Develop a program to compute and print in the order listed, the values of the following
eight Boolean expressions. They represent eight fundamental A, B, C combinations. NOTE the
resulting pattern.

P1 = A and B and C
P2 = A and B and (not C)
P3 = A and (not B) and C
P4 = A and (not B) and (not C)
P5 = (not A) and B and C
P6 = (not A) and B and (not C)
P7 = (not A) and (not B) and C
P8 = (not A) and (not B) and (not C)
ANSWER:
CLS
INPUT a
INPUT b
INPUT c
P1 = a AND b AND c
P2 = a AND b AND (NOT c)
P3 = a AND (NOT b) AND c
P4 = a AND (NOT b) AND (NOT c)
P5 = (NOT a) AND b AND c
P6 = (NOT a) AND b AND (NOT c)
P7 = (NOT a) AND (NOT b) AND c
P8 = (NOT a) AND (NOT b) AND (NOT c)
PRINT P1, P2, P3, P4, P5, P6, P7, P8
END

ACTIVITY 8-3
QUESTION: Suppose that NP naira was borrowed from the bank with understanding that NA will be
paid each month until the entire loan has been repaid. Part of the monthly payment will be interest
calculated as i% of the current un-paid balance. The remainder of the monthly payment would be
applied towards reducing the unpaid balance. Write a program that will determine the following
information.

a. The amount of interest paid each month.


b. The amount of money applied towards the unpaid balance each month.
c. The cumulative amount of interest that has been paid at the end of each month.
d. The amount of loan that is still unpaid at the end of the month.
e. The number of monthly payments required to repay the entire loan.
f. The amount of the last payment.

ANSWER:
CLS
REM program to calculate for a bank loan repayment and interest
INPUT "Enter the amount borrowed: ", NP
INPUT "Enter the amount to be paid monthly: ", NA
INPUT "Enter the interest rate (%)", I
BALANCE = NP
CUM_ITR = 0 'cumulative interest
COUNT = 1
WHILE BALANCE > 0
LAST_PAYMENT = 0
MNTLY_ITR = (I / 100) * BALANCE 'Monthly interest
PRINT "INTEREST PAYMENT FOR MONTH "; COUNT; " IS "; MNTLY_ITR
PRINT "THE AMOUNT OF UNPAID LOAN IS "; BALANCE
IF (BALANCE > NA) THEN
MNTLY_PAY = NA + MNTLY_ITR
ELSE
MNTLY_PAY = BALANCE + MNTLY_ITR
END IF
LAST_PAYMENT = MNTLY_PAY
PRINT "THE AMOUNT TO BE PAID THIS MONTH IS "; MNTLY_PAY
CUM_ITR = CUM_ITR + MNTLY_ITR
PRINT "THE CUMMULATIVE INTEREST PAID SO FAR IS "; CUM_ITR
IF (BALANCE >= NA) THEN
BALANCE = BALANCE - NA
ELSE
BALANCE = 0
END IF
COUNT = COUNT + 1
PRINT "---------------------------------------------------------------"
WEND
PRINT "THE NUMBER OF MONTHLY PAYMENT REQUIRED IS "; COUNT - 1
PRINT "THE AMOUNT OF LAST PAYMENT IS "; LAST_PAYMENT
END

ACTIVITY 8-4
QUESTION: A Pizza shop is planning to offer online purchasing. Pizza can be purchased in three
different sizes. The cost is N200 for a small Pizza, N320 for medium and N450 for large (plain with
cheese and Tomato sauced). The following toppings are available: Mushroom, pepper roil, sausage,
onions, green peppers, black olives and shrimp. Each topping is an addition N50 for a small pizza,
N100 for a medium pizza, N150 for large pizza. In addition, a customer can order for a supereme
with everything on it for N150, N200, and N270 small, medium and large respectively. Write a basic
program to

a. Allow the customer to specify the size and the choice of individual toppings
b. In addition include an option to order a supreme for each of the 3 sizes
c. Display the cost of the pizza, the state tax (assume a rate 12%) and the total cost as separate
data item

ANSWER:
CLS
REM program to sell pizza online
PRINT "WELCOME TO PIZZA SHOP! ORDER YOUR CHOICE"
PRINT "TO ORDER FOR A SUPREME PIZZA PRESS 1 ELSE PRESS 2 (0 TO EXIT)"
5 INPUT ": ", I
SELECT CASE I
CASE 0
GOTO 10
CASE 1
CALL SUPREME_PIZZA
CASE 2
CALL PIZZA
CASE DEFAULT
PRINT "INVALID INPUT"
GOTO 5
END SELECT
10 END
SUB SUPREME_PIZZA
STATE_TAX = 12 / 100
15 INPUT "SELECT SIZE: 1 FOR SMALL, 2 FOR MEDIUM, 3 FOR LARGE: ", SZ
SELECT CASE SZ
CASE 1
COST = 200 + 150
TOTAL_COST = COST + (COST * STATE_TAX)
PRINT "YOU ORDERD FOR SMALL SUPREME PIZZA AND COST IS "; COST
PRINT "TOTAL COST IS "; TOTAL_COST
CASE 2
COST = 320 + 200
TOTAL_COST = COST + (COST * STATE_TAX)
PRINT "YOU ORDERD FOR MEDIUM SUPREME PIZZA AND COST IS "; COST
PRINT "TOTAL COST IS "; TOTAL_COST
CASE 3
COST = 450 + 270
TOTAL_COST = COST + (COST * STATE_TAX)
PRINT "YOU ORDERD FOR LARGE SUPREME PIZZA AND COST IS "; COST
PRINT "TOTAL COST IS "; TOTAL_COST
CASE DEFAULT
PRINT "INVALID INPUT"
GOTO 15
END SELECT
END SUB

SUB PIZZA
STATE_TAX = 12 / 100
INPUT "SELECT SIZE: 1 FOR SMALL, 2 FOR MEDIUM, 3 FOR LARGE: ", SZ
SELECT CASE SZ
CASE 1
COST = 200
PRINT "YOU ORDERED A SMALL SIZED PIZZA"
CASE 2
COST = 320
PRINT "YOU ORDERED A MEDIUM PIZZA"
CASE 3
COST = 450
PRINT "YOU ORDERED A LARGE PIZZA"
END SELECT
PRINT "TOPPINGS LIST SELECTION: 1 = MUSHROOM, 2 = PEPPER ROIL, 3 = SAUSAGE,";
PRINT " 4 = ONIONS, 5 = GREEN PEPPER, 6 = BLACK OLIVES, 7 = SHRIMP"
DO
INPUT "SELECT THE TOPPING VALUE (0 TO STOP OR TO ORDER PLAIN): ", TOP
IF (TOP <> 0) THEN
IF SZ = 1 THEN COST = COST + 50
IF SZ = 2 THEN COST = COST + 100
IF SZ = 3 THEN COST = COST + 150
END IF
LOOP WHILE TOP <> 0
TOTAL_COST = COST + (COST * STATE_TAX)
PRINT "COST = "; COST
PRINT "TOTAL COST = "; TOTAL_COST
END SUB

ACTIVITY 8-5
QUESTION: The Gregorian Epact is the number of days between Jan 1st and the previous 1st quarter
moon phase. The value is obtained in order to figure out the date of Easter. It is calculated by these
formulas. (Using int arithmetic) : C = year/100
Epact = (8+(C/4)-C+((8C+13)/25)+11(year%19))%30. Write a basic program that prompts the user for
4 digit year and then outputs the value of the epact
ANSWER:
CLS
REM program to calculate EPACT
INPUT "ENTER A FOUR DIGIT YEAR: "; YEAR
C% = YEAR / 100
PRINT C%
EPACT = (8 + (C% / 4) - C% + (((8 * C%) + 13) / 25) + (11 * (YEAR MOD 19))) MOD 30
PRINT "EPACT = "; EPACT
END

ACTIVITY 8-7
QUESTION: The sequence of Fibonacci numbers begins thus: 1,1,2,3,5,8,13,21,34, and continues
endlessly, each number begins the sum of the preceding two. Prepare a basic program to compute
the first 25 numbers together with the ratio of each to the one before it.
ANSWER:
CLS
REM program to display the first 25 fibonacci number.
C1 = 1
C2 = 1
C3 = 0
PRINT C1
PRINT C2
FOR I = 1 TO 23
C3 = C1 + C2
PRINT C3; " RATIO = "; C3; ":"; C2
C1 = C2
C2 = C3
NEXT I
END

ACTIVITY 8-9
QUESTION: Write a basic program to check if a number is palindrome or not.

Note: a palindrome number is a number that remains the same when its digits are reversed. E.g.
16261, 191, 2, 77, 999.
ANSWER:
INPUT "ENTER A NUMBER: ", NUM$
B_NUM$ = ""
FOR I = LEN(NUM$) TO 1 STEP -1
B_NUM$ = B_NUM$ + MID$(NUM$, I, 1)
NEXT
IF (NUM$ = B_NUM$) THEN
PRINT NUM$; " IS PALINDROME"
ELSE
PRINT NUM$; " IS NOT PALINDROME"
END IF
END

NOTE: Please try the ACTIVITIES that are not included in this solution manual.

Draw the flowcharts for the activities that require one.

Read and attempt the other questions included in the practical manual.

Compiled By: Olayinka Okewale (RCF Academic Unit, FUTA)

Das könnte Ihnen auch gefallen