BASIC PROGRAMMING LANGUAGE II (SS2 3RD TERM)

BASIC PROGRAMMING LANGUAGE II

Outline

=> BASIC Built-in Functions

=> BASIC Notation/Symbols

=> BASIC Programs to Solve Problems

BUILT-IN FUNCTIONS

Function

A function (built-in) is a built-in formula used to accomplish certain task such as mathematical, statistical, financial, and logical/data calculations, etc.

Functions are readymade programs, in BASIC, which take some data, manipulate them and return the value, which maybe string type (alphanumeric) or numeric type (mathematical).

A function Is a subprogram designed to perform a specific task and return a value.

Built -in functions

Built - functions are functions built into an application and can be accessed by end users.

Built-in functions are similar to operation codes in that they perform operations on data you specify. They can be used in expressions.

 

LIST OF SOME BASIC BUILT - IN FUNCTIONS

1) SQR function - SQR (X): 

The SQR is used to calculate the square root of a number.

E.g. SQR (4) is 2; SQR (16) is 4; SQR (2) is 1.41421.

Example: Write a program to calculate x=√ab, if a=4, and b=9

10  CLS

20  LET A=4

30  LET B=9

40  X=SQR (A*B)

50  PRINT X

60  END

2) INT function-INT(X)

This function will find the greatest integer that is less than or equal to the number given,(X). INT throw (chop off) the decimal part of the numbers.

E.g. INT(10.2) is 10; INT(5.2) is 5; INT(6.2) is 6

Example: Write a program to calculate y=INT (B) if B=13.5

10  CLS

20  LET B=13.7

30  Y=INT (B)

40  PRINT Y

50  END

3) ABS function-ABS (X): 

This math function returns the absolute value of a numeric expression.

A number's absolute is its value without a "+" or "-". (it also returns -ve number into +ve number).

E.g ABS (-9.76) is 9.76; ABS (+23.456) is 23.456

Example:

Write a program to calculate K=ABS (R) if R=23.6

10  CLS

20  LET R=23.6

30  K=ABS (R)

40  PRINT K

50  END

4) RND function-RND(X):

This function returns (produce) a random number between 0 and 1.

e.g to produce a random number (integer) in the range of 0 and 1,

It's 0.99, etc.

5) MOD function-MOD(X):

This function returns (produce) the remainder when one number is divided by another.

E.g 10 MOD 3=1, 21 MOD 6=3

Example:

Write a program to calculate the remainder when x is divided by y given that x=35, and y=5.

10  CLS

20  LET x=35

30  LET y=5

40  Z=35 MOD 5

50  PRINT Z

60  END

6) CINT function-CINT(X): This function is used to convert a number into an Integer. It rounds up the number to the nearest integer.

E.g CINT (23.39)=23, CINT (15.83)=16, CINT (23.8)=24

Example:

Write a program to calculate the value of Y=CINT (18), if X=19.39

10  CLS

20  LET x=19.36

40  Y=CINT (x)

50  PRINT Y

60  END

TRIGONOMETRIC FUNCTIONS

The BASIC trig functions are:

1) COS-COS (X): cosine in radian .it gives the Cosine of a number.

Cosine => COS (X) e.g. COS (50 degrees)

2) SIN-SIN(X): sine in radian. It gives the Sine of a number.

Sine => SINE (X) e.g. SINE (45 degrees)

3) TAN-TAN (X): tangent in radian. It gives the Tangent of a number.

Tangent => TAN (X) e.g. TAN (50 degrees)

Example:

Write a program to calculate the Cosines, Sines, and Tangent of x, if x=60 degrees.

10  CLS

20  LET x=60

30  PRINT COS (X),  SIN (X), TAN (X)

60  END

 

LOGARITHMIC FUNCTIONS

1) LOG-LOG(X): Log returns (produce) the natural (base e) log of a number instead of the base 10 log.

2) EXP-EXP(X): It raises 'e' to the number power. 'e' is the base of the natural log, which is about 2.18281828.

Obviously, EXP can be used to calculate 'e'.

Assignment

1. Write a program to calculate y=√4ac, when a=5, c=10

2. Write a program to calculate the remainder when c is divided by d, given that c=9, d=3

BASIC Notation/Symbols

(i)√b2 -4ac/2a

Solution: ((b^2-4*a*c)^(1/2))/(2*a) or ((b**2-4*a*c)^(1/2))/(2*a)

(ii) x-y/x+y

Solution: (x-y)/(x+y)

(iii) (a+b) + c/Sin d

Solution: (a+b) +c / Sin (d)

(iv) ex+y –Sin (x +ny)

Solution: (e^x+y- Sin(x+n*y))

(v) b = 1/4ac

Solution: b=1/4 (a*c)

(vi) A=∏r²

Solution: PI*r**2 or PI*r^2

EVALUATION

What is the meaning of these BASIC functions? LOG, TAN, and ABS.

Which Mathematical operator has two notations in BASIC?

BASIC PROGRAM TO SOLVE PROBLEMS

Simple Program #1

10 REM PROGRAM TO COMPUTE THE SQUARE ROOT OF NOS 10 TO 40

20 FOR A = 10 TO 40

30 PRINT A; SQR(A)

40 NEXT A

50 END

 

Simple Program #2

10 REM PROGRAM TO DISPLAY SIN, COS AND TAN 30, 45, AND 60

20 PRINT, 30, 45, 60

30 LET PI = 3.141592

40 PRINT “SIN”, SIN(30*(PI/180)), SIN(45*(PI/180)), SIN(60*(PI/180))

50 PRINT “COS”, COS(30*(PI/180)), COS(45*(PI/180)), COS(60*(PI/180))

60 PRINT “TAN”, TAN(30*(PI/180)), TAN(45*(PI/180)), TAN(60*(PI/180))

70 END

Comments

Popular posts from this blog