Monday 4 February 2013

VbScript Programs



1. Write a VbScript Program  for factorial of n?

n=cint(inputbox("Enter a number"))
dim f
f=1
 for i=n to 1 step -1
    f=f*i
next
 MsgBox "The factorial of given number "&n&" is :"&f 

2.Write a vbscript program to find Fibonacci series?

Dim a,b,fib,n
a=0
b=1
n = inputbox("Enter the number")
msgbox a
' Logic is 0 1 1 2  3 5 8
for i=1 to n
fib = a+b
msgbox fib
a=b
b=fib
Next 

3. Write a program to  retrive the nueric values only in main string?

  Dim strname,strlength
  strname = "asfgsq123zvd2312xvcxv333c 312v23rv2vfxb3456"
  strlength = Len(strname)
  msgbox strlength
 For i = 1 to strlength
          ch = mid(strname,i,1)
             num = isnumeric(ch)
          If num = true Then
                   cha = cha&ch
                   Reporter.ReportEvent micDone,"numeric values",cha
          End If
 Next

4. Write a program to reverse the string without using StrReverse function?

   Dim strname, strlength
   strname = "This function is to reverse the tring without using strreverse"
  strlength = len(strname)
  For i = strlength to 1 step-1
          ch = mid(strname,i,1)
          cha = cha&ch
    reporter.ReportEvent micDone,"Reverse string",cha
 Next

5. Write a vbscript program to display the multiple Tables?

            Dim x,i,y,product,j,table
            x=inputbox("enter the number ")
            For i=1 to x
               For j=1 to 10
                  product=i*j
                  x= i&"*"&j&"="&product
                  table=table&vbnewline&x
               Next
             Next
             msgbox table

6.Find the factorial of a given number? 
   
                 Dim Number,Counter,fValue
                Number=6
                fValue=1
                For Counter=Number to 1 step-1
                      fValue=fValue*Counter
               Next
               Msgbox fValue

7. Find whether given number is a odd number?

                Dim Number
                Number=4
                If oNumber mod 2 <>0 Then
                     Msgbox  "The Number "& Number &" is an Odd Number"
                else
                     msgbox  "The Number "& Number &" is not an Odd Number"
                End If

8. find odd numbers between given range of numbers?
   
Dim RangeStart,RangeEnd   
RangeStart=10   
RangeEnd=20   
For Counter=RangeStart to RangeEnd   
    If Counter mod 2 <>0 Then  
        Msgbox  Counter  
    End If  
Next  

9. Find the factors of a given number?   

Dim Number,Counter   
  Number=10   
  For Counter=1 to Number  
    If Number mod Counter=0 Then  
        Msgbox Counter   
    End If  
Next  
Msgbox  Number 

10. Swap 2 numbers without a temporary variable?    

Dim Num1   
Dim Num2   
Num1=1055   
Num2=155   
Num1=Num1-Num2   
Num2=Num1+Num2   
Num1=Num2-Num1   
Msgbox  Num1   
Msgbox  Num2  

11. Write a VbScript program to display the below n prime numbers?
 
Dim i,j,n
n=inputbox("enter a number")
For  i=2 to n
    count=0
    For j = 1 to i
      If i mod j =0 Then
        count=count+1
    End If
Next
   If count = 2 Then
     num =i
     prime = prime&space(2)&num
   End If
Next
msgbox prime

12. Find the length of a given string?

Dim Str,Length   
Str=" Quality Thought "  
Length=len(Str)   
Msgbox  Length  

13.  Reverse given string?   

Dim Str,Length,Char,Counter   
Str=" Quality Thought "  
Length=len(Str)   
 For Counter=Length to 1 step-1   
        Char=Char&mid(Str,Counter,1)   
Next  
Msgbox  Char   

14.Write a program to Perform specified Arithmetic Operation on two given              numbers?  

Dim Num1   
Dim Num2   
Dim Value   
Num1=10   
Num2=20   
OperationtoPerform="div"  
Select Case lcase(OperationtoPerform)   
                Case "add"  
                                Value=Num1+Num2   
                Case "sub"  
                                Value=Num1-Num2   
                Case "mul"  
                                Value=Num1*Num2   
                Case "div"  
                                Value=Num1/Num2   
End Select  
Msgbox Value   

15. Find how many alpha characters present in a string?   

Dim Str,Length,Char,Counter   
Str=" Quality Thought "  
Length=len(Str)   
Alphacounter=0   
 For Counter=1 to Length   
    If not isnumeric (mid(Str,Counter,1)) then   
        Alphacounter=Alphacounter+1    
    End if   
Next  
Msgbox  Alphacounter 

16. Find occurrences of a specific character in a string 

Dim Str,Array,chr   
Str="Quality Thought"  
chr="a"  
 Array=split(Str,chr)   
msgbox ubound(Array)

17. Replace space with tab in between the words of a string
  
 Dim Str,fStr  
Str="Quick Test Professional"  
fStr=replace(Str," ",vbtab)   
msgbox  fStr 

18. Write a program to return ASCII value of a given character 
  
Dim chr,Val   
chr="A"    
Val=asc(chr)   
msgbox Val 

19.Write a program to return character corresponding to the given ASCII value 

Dim chr,Val
Val=65
Chr=chr(Val)   
msgbox Chr  

20. Write a program to Replace a word in a string with another word
   
 Dim Str,Word1,Word2,fStr  
 Str="Mercury Quick Test Professional"  
Word1="Mercury"  
Word2="HP"  
fStr=replace(Str,Word1,Word2)   
msgbox  fStr    

21. Check whether the string is a POLYNDROM   

Dim Str    
Str="bob"  
fStr=StrReverse(Str)    
If Str=fStr Then  
    Msgbox  "The Given String "&Str&" is a Palindrome"  
    else   
    Msgbox "The Given String "&Str&" is not a Palindrome"  
End If  

22. Print all values from an Array   

Dim Arry,Counter   
Arry = array(1,2,3,4,"qtp","Testing")   
For Counter=lbound(Arry) to ubound(Arry)   
    Msgbox  Array(Counter)   
Next  

23. Sort Array elements   

Dim Arry,Counter1,Counter2,tmp   
  Arry=Array(8,3,4,2,7,1,6,9,5,0)   
  For Counter1=lbound(Arry) to ubound(Arry)   
     For Counter2=lbound(Arry) to ubound(Arry)-1   
         If Arry(Counter2)>Array(Counter2+1) Then
                       tmp=Array(Counter2)   
                        Arry(Counter2)=Arry(Counter2+1)   
                        Arry(Counter2+1)=tmp   
                    End If   
        Next  
Next  
For Counter1=lbound(Arry) to ubound(Arry)   
    Msgbox  Arry(Counter1)   
Next  

24. Add two 2X2 matrices  

Dim Arry1(1,1),Arry2(1,1), tArry(1,1)  
Arry1(0,0)=8   s
Arry1(0,1)=9   
Arry1(1,0)=5   
Arry1(1,1)=-1   

Arry2(0,0)=-2   
Arry2(0,1)=3   
Arry2(1,0)=4   
Arry2(1,1)=0   
  
tArry(0,0)=Arry1(0,0)+ Arry2(0,0)   
tArry(0,1)=Arry1(0,1)+Arry2(0,1)   
tArry(1,0)=Arry1(1,0)+Arry2(1,0)   
tArry(1,1)=Arry1(1,1)+Arry2(1,1)   

25. Convert a String in to an array   

Dim Str,Counter   
Str="Quick Test Professional"  
StrArray=split(Str)   
  
For Counter=0 to ubound(StrArray)   
       Msgbox  StrArray(Counter)   
Next  

26.Multiply Two Matrices of size 2X2   

Dim Arry1(1,1),Arry2(1,1),tArry(1,1)  
Arry1(0,0)=8   
Arry1(0,1)=9   
Arry1(1,0)=5   
Arry1(1,1)=-1   
Arry2(0,0)=-2   
Arry2(0,1)=3   
Arry2(1,0)=4   
Arry2(1,1)=0   
  
tArry(0,0)=Arry1(0,0)* Arry2(0,0)+Arry1(0,1)*Arry2(1,0)   
tArry(0,1)=Arry1(0,0)*Arry2(0,1)+Arry1(0,1)*Arry2(1,1)   
tArry(1,0)=Arry1(1,0)*Arry2(0,0)+Arry1(1,1)*Arry2(1,0)   
tArry(1,1)=Arry1(1,0)*Arry2(0,1)+Arry1(1,1)*Arry2(1,1) 

 27. Convert a String in to an array using ‘i‘ as delimiter  

Dim Str,Counter   
Str="Quick Test Professional"  
StrArray=split(Str,"i")   
For Counter=0 to ubound(StrArray)   
        Msgbox  StrArray(Counter)   
Next   

28. Find number of words in string 
  
Dim Str,Counter   
Str="Quick Test Professional"  
StrArray=split(Str," ")   
Msgbox  "Theere are "&ubound(StrArray)+1&" words in the string"      

29.   Print the data as a Pascal triangle 
  
'The formulae for pascal triangle is nCr=n!/(n-r)!*r!   
Dim PascalTriangleRows,nCr,NumCount,RowCount   
PascalTriangleRows = 10   
For NumCount = 0 To PascalTriangleRows   
    toPrint= Space(PascalTriangleRows - NumCount)   
    For RowCount = 0 To NumCount   
            If (NumCount = RowCount) Then  
                    nCr = 1   
            Else  
                nCr = Factorial(NumCount) / (Factorial(NumCount - RowCount) * Factorial(RowCount))   
            End If  
     toPrint = toPrint&nCr&" "  
    Next  
    Msgbox  toPrint   
Next  
Function Factorial(num)   
   Dim iCounter   
    Factorial = 1   
    If num <> 0 Then  
        For iCounter = 2 To num   
            Factorial = Factorial * iCounter   
        Next  
    End If  
End Function  

30. Join elements of an array as a string 
  
Dim Str,Counter
Str="Quick Test Professional"  
StrArray=split(Str," ")  
Msgbox  join(StrArray," ")  

31. Write a program to insert 100values and to delete 50 values from an array   

Dim Arry(),Counter   
ReDim Arry(100)   
For Counter=0 to ubound(Arry)   
            Arry(Counter)=Counter   
            'Message the total 100 Values   
            Msgbox (Arry(Counter))   
Next   
ReDim preserve Arry(50)   
For Counter=0 to ubound(Arry)   
    'Message the Values after deleting 50 values   
            Msgbox (oArray(iCounter))   
Next 

32. Write a program to raise an error and print the error number.  
  
On Error Resume Next  
Err.Raise 6   ' Raise an overflow error.   
Msgbox  ("Error # " & CStr(Err.Number) & " " & Err.Description)   

33. Finding whether a variable is an Array   

          Dim Arry()
if  isarray(Arry) then
Msgbox  "the given variable is an array"
Else
  Msgbox  "the given variable is not an array"  
End if   

34.  Write a program to Convert an expression to a date   

Dim StrDate,actualDate,StrTime,actualTime   
StrDate = "October 19, 1962"   ' Define date.   
actualDate = CDate(StrDate)   ' Convert to Date data type.   
Msgbox  actualDate   
StrTime = "4:35:47 PM"         ' Define time.   
actualTime = CDate(StrTime)   ' Convert to Date data type.   
Msgbox  actualTime

35. Find whether current month is a long month 
  
Dim CurrentMonth,currentYear,DaysinMonths     
CurrentMonth = Month(date)   
currentYear = Year(date)   
DaysinMonths=Day(DateSerial(currentYear, CurrentMonth + 1, 0))   
msgbox  DaysinMonths&" Days in Current Month"If DaysinMonths=31 Then  
    Msgbox  "Current Month is a long month"  
else   
    Msgbox  "Current Month is not a long month"  
End If

36. Format Number to specified decimal places   

Dim Num,DecimaPlacestobeFormat   
Num = 3.14159   
DecimaPlacestobeFormat=2   
Msgbox  Round(oNum , DecimaPlacestobeFormat)

36. Write a program to  find subtype of a variable  

Dim oVar   
Dim oDatatypes   
oVar="QTP"  
oVartype=Typename(oVar)   
msgbox  oVartype   

37. Find whether given year is a leap year   
'1st Method 

'The rules for leap year:   
'1. Leap Year is divisible by 4    (This is mandotory Rule)   
'2. Leap Year is not divisible by 100 (Optional)   
'3. Leap Year divisble by 400 (Optional)   
  
Dim oYear   
oYear=1996   
If ((oYear Mod 4 = 0) And (oYear Mod 100 <> 0) Or (oYear Mod 400 = 0)) then   
    Msgbox  "Year "&oYear&" is a Leap Year"  
else   
    msgbox  "Year "&oYear&" is not a Leap Year"  
End If  

'2nd Method   

' Checking 29 days for February month in specified year   
Dim oYear,tmpDate   
oYear=1996   
tmpDate = "1/31/" & oYear   
DaysinFebMonth = DateAdd("m", 1, tmpDate)   
If  day(DaysinFebMonth )=29 then   
    Msgbox  "Year "&oYear&" is a Leap Year"  
else   
    msgbox  "Year "&oYear&" is not a Leap Year"  
End If 



38. Write a program to Generate a Random Numbers   

'This script will generate random numbers between 10 and 20   
Dim RstartRange,RendRange  
RstartRange=10   
RendRange=20   
  
For iCounter=1 to 10   
    Msgbox  Int((RendRange - RstartRange + 1) * Rnd + RstartRange)   
          Next 

39.  Write a program to show difference between Fix and Int  
  
'Both Int and Fix remove the fractional part of number and return the resulting integer value.   
'The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number,   
'whereas Fix returns the first negative integer greater than or equal to number.    
'For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.   
  
Msgbox Int(99.8)    ' Returns 99.   
Msgbox Fix(99.2)    ' Returns 99.   
Msgbox Int(-99.8)   ' Returns -100.   
Msgbox Fix(-99.8)   ' Returns -99.   
Msgbox Int(-99.2)   ' Returns -100.   
Msgbox  Fix(-99.2)   ' Returns -99.   

40. Write a program to print the decimal part of a given number   

Dim oNum   
oNum=3.123   
oDecNum=oNum- int(oNum)   
msgbox  oDecNum   

No comments:

Post a Comment