Solution of FUNCTION Procedure1

1. Write a program to declare a user defined function to return sum of any digits of number by using FUNCTION...END FUNCTION.

DECLARE FUNCTION sum(N)
CLS
INPUT "Enter a number";N
PRINT "Sum of digits of a number is ";sum(N)
END

FUNCTION sum(N)
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
sum=S
END FUNCTION

2. Write a program to count total number of alphabet "A" occurred in a string, where string is passed as a parameter by using FUNCTION...END FUNCTION.

DECLARE FUNCTION countA(S$)
CLS
INPUT "Enter a string";S$
PRINT "Total number of A occurred in a string is ";countA(S$)
END

FUNCTION countA(S$)
FOR I=1 TO LEN(S$)
IF UCASE$(MID$(S$,I,1))="A" THEN
c=c+1
END IF
NEXT I
countA=c
END FUNCTION

3. Write a program to print Armstrong number between 1 to 2000 by using FUNCTION....END FUNCTION.

DECLARE FUNCTION display( )
CLS
v=display
END

FUNCTION display
FOR I=1 TO 2000
S=0
N=I
WHILE N<>0
R=N MOD 10
S=S+R^3
N=N\10
WEND
IF S=I THEN
PRINT I,
END IF
NEXT I
END FUNCTION


4. Write a program that supplies array of 10 different numbers, find sum of odd numbers using FUNCTION...END FUNCTION.

DECLARE FUNCTION sum (N( ))
CLS
DIM N(10)
FOR i = 1 TO 10
INPUT "Enter a number"; N(i)
NEXT i
PRINT "Sum of odd number is "; sum(N( ))
END


FUNCTION sum (N( ))
FOR i = 1 TO 10
IF N(i) MOD 2 <> 0 THEN
s = s + N(i)
END IF
NEXT i
sum = s
END FUNCTION

5. Write a program to display following series:
2,1,3,4,7,11,18....up to the 10th term by using FUNCTION...END FUNCTION.

DECLARE FUNCTION series ()
CLS
v = series
END

FUNCTION series
a = 2
b = 1

FOR i = 1 TO 10
PRINT a,
c = a + b
a = b
b = c
NEXT i

END FUNCTION


6. Write a program to find the reverse string of a given string by using FUNCTION...END FUNCTION.

DECLARE FUNCTION reverse$ (S$)
CLS
INPUT "Enter a string "; S$
PRINT "The reverse string is "; reverse$(S$)

END

FUNCTION reverse$ (S$)
FOR I = LEN(S$) TO 1 STEP -1
R$ = R$ + MID$(S$, I, 1)
NEXT I
reverse$ = R$
END FUNCTION

7. Write a program to concatenate (link) three different strings input by a user as illustrated below in the hint given using a user defined function named strcon( ). [Hint: if the input string are "FIRST", "SECOND" and "THIRD" Then the output should be "FIRSTSECONDTHIRD"]

DECLARE FUNCTION strcon$ (F$, S$, T$)
CLS

INPUT "Enter First string"; F$
INPUT "Enter second string"; S$
INPUT "Enter third string"; T$
PRINT "The concatenated string is "; strcon$(F$, S$, T$)
END


FUNCTION strcon$ (F$, S$, T$)
strcon$ = F$ + S$ + T$
END FUNCTION

8. Write a program to print the following output using FUNCTION...END FUNCTION.

N
NE
NEP
NEPA
NEPAL

DECLARE FUNCTION show ()
CLS
v = show

END



FUNCTION show
S$ = "NEPAL"
FOR i = 1 TO LEN(S$)
PRINT LEFT$(S$, i)
NEXT i
END FUNCTION





Comments

Popular posts from this blog

input any string and reverse it, palindrome

PALINDROME AND ARMSTRONG PROGRAMS

N NE NEP NEPA NEPAL PATTERN