Friday, April 16, 2010

Assignment Answer on Functions.

Back to the Previous Page

Write a script in VB to Convert all the previous written scripts like Fibonacci series in VB Script, Prime number Generation in VB Script, Odd number generation in VB Script, conversion from centigrade to Fahrenheit in VB Script, Addition in VB Script, Multiplication in VB Script etc using functions in VB Script.


In order to write this script first we need to use a inputbox to ask the user the choice to perform a certain function. Then Use Select Case taking the value from Inputbox and based on the condition it will select the case in which the function call is specified.After the Select Case Write down all the Functions for different operations.

The code looks like this

Here you are asking the user for the choice to perform the operation.

n=Inputbox("Enter your choice"&vbnewline&" "&vbnewline&"1 - Addition"&vbnewline&
"2 - Subtraction"&vbnewline&"3 - Multiplication"&vbnewline&"4 - Quotient"&vbnewline&
"5 - Fibonacci Series"&vbnewline&"6 - Odd Numbers"&vbnewline&"7 - Prime Numbers"&vbnewline&"8 - Degree Conversion")

Here you are selecting the case based on the user input and appropriately calling the required function.

Select Case n
Case 1
aaaaddfunc x,y
Case 2
aaaminusfunc x,y
Case 3
aaamultiply x,y
Case 4
aaaquotient x,y
Case 5
aaaFibonacci var
Case 6
aaaoddfunc var
Case 7
aaaprime var
case 8
aaaconversion cen, fah
Case Else
aaamsgbox "invalid entry"
End Select

Here you are actually defining the functions for different operations to be performed.

For Addition of two Numbers

Function addfunc(byval x, byval y)
aaax = CINT(inputbox("Enter 1st number to be added"))
aaay = CINT(inputbox("Enter 2nd number to be added"))
aaamsgbox "The summation of "&x& " and " &y& " is: " &x+y
End Function


For Subtraction of two Numbers

Function minusfunc(byval x, byval y)
aaax = CINT(inputbox("Enter 1st number"))
aaay = CINT(inputbox("Enter 2nd number to be subtracted"))
aaamsgbox "The difference between "&x& " and " &y& " is: "&x-y
End Function


For Multiplication of two Numbers

Function multiply(byval x, byval y)
aaax = CINT(inputbox("Enter 1st number to be multiplied"))
aaay = CINT(inputbox("Enter 2nd number to be multiplied"))
aaamsgbox "The product of "&x& " and " &y& " is: "&x*y
End Function

For finding quotient of two Numbers

Function quotient (byval x, byval y)
aaax = CINT(inputbox("Enter 1st number to be divided"))
aaay = CINT(inputbox("Enter 2nd number as divisor"))
aaamsgbox "The quotient between "&x& " and " &y& " is: "&x\y
End Function


For displaying the Fibonacci Series of a given Number

Function Fibonacci (byval var)
aaan = CINT(Inputbox("Enter a range for the Fibonacci Series"))
aaaf1=0
aaaf2=1
aaaf3=f1+f2
aaavar= f1& ", " &f2
aaaaaado while f3 < n
aaaaaaaaavar =" var&", "&f3
aaaaaaaaaf1=f2
aaaaaaaaaf2=f3
aaaaaaaaaf3=f1+f2
aaaaaaLoop
MsgBox("Fibonacii Series for the limit " &n&" is: " &var)
End Function


For displaying the Odd Numbers of a given Number

Function oddfunc(byval var)
aaan = CINT(Inputbox("Enter the limit for the Odd Numbers"))
aaaaaaFor i = 1 to n
aaaaaaaaaif (i mod 2<>0) then
aaaaaaaaavar = "" &var&", " &i
aaaaaaaaaEnd If
aaaaaanext
aaaMsgBox("Odd numbers within " &n&" numbers = :" &var)
End Function


For displaying the Prime numbers of a given Number

Function prime(byval var)
n = inputbox("Please Enter the limit for Prime Numbers to be printed")
aaaFor i = 1 to n
aaaaacount = 0
aaaaaa = 1
aaaaaaaDo While a<=i
aaaaaaaaaif i mod a = 0 Then
aaaaaaaaaaacount = count + 1
aaaaaaaaaend if a=a+1
aaaaaaaLoop
aaaaaaaaaIf count = 2 Then
aaaaaaaaaaavar = var&" " &i& ", "
aaaaaaaaaEnd If
aaaNext
aaaMsgBox("Prime numbers within " &n&" numbers = :" &var)
End Function


For converting from and to Centigrade or Fahrenheit of a given Number

Function conversion(byval cen, byval fah)
con = inputbox("Enter 1 for Centigrade to Fahrenheit and 2 for Fahrenheit to Centigrade")
aaaSelect Case con
aaaaaCase 1
aaaaaaacen=inputbox("Enter the value to be converted")
aaaaaaamsgbox "The converted value of " &cen& " Centigrade is " &cen\0.55555555556 + 32& " Fahrenhite"
aaaaaCase 2
aaaaaaafah=inputbox("Enter the value to be converted")
aaaaaaamsgbox "The converted value of " &fah& " Fahrenheit is " &(fah-32) *0.55555555556 & " Centigrade"
aaaaaCase Else
aaaaaaamsgbox "Invalid entry for Conversion"
aaaEnd Select
End Function


Back to the Previous Page

No comments:

Post a Comment