0% found this document useful (0 votes)
8 views6 pages

Grade 13 Option C (Muckoon)

The document outlines several programming functions including PRODUCT, AZT, AreaofCircle, TAX, and recursive functions for calculating factorial and Fibonacci numbers. It explains the concept of recursion, emphasizing the need for a base case and a general case to prevent infinite calls. Additionally, it provides examples of recursive procedures and their implementations in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Grade 13 Option C (Muckoon)

The document outlines several programming functions including PRODUCT, AZT, AreaofCircle, TAX, and recursive functions for calculating factorial and Fibonacci numbers. It explains the concept of recursion, emphasizing the need for a base case and a general case to prevent infinite calls. Additionally, it provides examples of recursive procedures and their implementations in code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

OPTION C

14 JAN 2025

19.2 Recursion

Present : Muckoon, Nobin

1. Write a function PRODCUT to calculate the product of two values.

FUNCTION PRODUCT (BYVAL VALUE1 AS INTEGER, BYVALUE VALUE2 AS INTEGER) AS INTEGER

DIM Product as integer

Product = VALUE1 * VALUE2


Function Header
RETURN Product

END FUNCTION

2. Write a function AZT to calculate


- the product of two values if they are below 20
- Else sum the two values

FUNCTION AZT (BYVAL VALUE1 AS INTEGER, BYVALUE VALUE2 AS INTEGER) AS INTEGER

DIM ProductORsum as integer

IF ( VALUE1 < 20) AND (VALUE2 < 20) THEN

ProductORsum = VALUE1 * VALUE2

ELSE

ProductORsum = VALUE1+ VALUE2

ENDIF

RETURN ProductORsum

END FUNCTION

3. Write a function AreaofCircle to calculate the area of a circle

FUNCTION AreaofCircle ( BYVAL Radius as DOUBLE) as DOUBLE

Dim Area as DOUBLE


IF Radius < 0 THEN

Console.Writeline (“Incorrect value of radius”)

ELSE

Area = 3.14 * Radius^2

ENDIF

RETURN Area

END FUNCTION

4. Write a function TAX to calculate the tax based on the following table.

Salary above 100000 tax is 10% of salary

Salary between 80000 and 99999 tax is 8% of salary

Otherwise it is 5% of salary

FUNCTION TAX ( BYVALUE SALARY AS INTEGER) AS DOUBLE

IF SALARY > 100000 THEN

TAX = 0.1 * SALARY

ELSE

IF (SALARY >= 80000) AND (SALARY <= 99999) THEN

TAX = 0.08 * SALARY

ELSE

TAX = 0.05 * SALARY

ENDIF

ENDIF

END FUNCTION

4. Write a FUNCTION NUMA to calculate the number of digit 4 in a given integer.


21 JAN 2025

RECURSIVE FUNCTION

To derive a recursive function to calculate the binomial number of an integer.

To know the terms base case and general case.

EXPLANATION

Recursion is a process using a function or procedure that is defined in terms of


itself and calls itself. The process is defined using a base case, a terminating
solution to a process that is not recursive, and a general case, a solution to a
process that is recursively defined.

What IS A RECURSIVE FUNCTION?

 It is one which calls itself.


 It must have a base case and a general case.
 Without the base case, the function might call itself infinitely.

Example Factorial.

Factorial(0) =1

Factorial(1) =1

Factorial(2) = 1x2 = 2

Factorial(3) =1X2x3 = 3 X Factorial(2)

Factorial(4) = 1X2X3X4 = 4X Factorial(3)

Factorial(5) = 1X2X3X4X5 = 5 X Factorial(4)

Factorial(N) =N X F(N-1)

FUNCTION factorial ( BYVAL N as INTEGER) as INTEGER

IF N = 0 OR N = 1 THEN
Base
Factorial = 1 Case

ELSE General Case

Factorial = N * Factorial(N-1)

Endif
END FUNCTION

SUB main()

Dim F, factorialof as integer

Console.Writeline (“Please enter a number”)

F = Console.Readline()

Factorialof= Factorial(F)

Console.Writeline (“The factorial of ” & F & “is ”& Factorialof)

Console.Writeline (“Press Any Key to Continue”)

Console.readkey()

End Sub

Example 2

FIBONACCI

M 1 2 3 4 5 6 7 8 M
Fibonacci (M) 1 1 2 3 5 8 13 21

Base Case:

IF (M=1) OR (M =2) THEN

Fibonacci = 1

General Case

Fibonacci (M) = Fibonacci (M-1) + Fibonacci (M-2)

FUNCTION factorial ( BYVAL N as INTEGER) as INTEGER

IF (M=1) OR (M =2) THEN

Fibonacci = 1

Else

Fibonacci (M) = Fibonacci (M-1) + Fibonacci (M-2)


Endif

END FUNCTION

Example 3

Write a recursive procedure to output a specific message P number of times input by the user

Message :”Nobin” and P is 5

Output will be

Nobin

Nobin

Nobin

Nobin

Nobin

Sub Printing ( BYVAL msg as STRING, P)

IF P = 1 THEN

Console.Writeline(msg)

ELSE

Console.Writeline(msg)

CALL Printing(msg, P -1)

ENDIF

End sub

You might also like