UFT/QTP – VBScript Tutorial Solved Examples

Find whether the given number is an odd or even number
Dim n
n= inputbox(“enter the number”)
n =cint(n)
if (n mod 2 = 0) Then
print(n & “is even number”)
else
print(n & “is odd number”)
End If

Print odd numbers between a given range of numbers
Dim N1,N2,N,OddNumbers
N1 = inputbox(“Please enter the starting number”)
N2 = inputbox(“Please enter the ending number”)
N1 = Cint(N1)
N2 = Cint(N2)
Print(“The odd numbers between ” & N1 & ” and ” & N2 & ” are “)
OddNumbers = “”
for N=N1 to N2
if N mod 2 <> 0 then
OddNumbers = OddNumbers & cstr(N) & vbtab
End If
Next
print (OddNumbers)

Find the factorial of a given number
Dim N, factorial
N=inputbox(“Please enter the name”)
N=Cint(N)
factorial =1
index = N
if index<0 Then
print(“No factorial for negative numbers”)
elseif (index=0) or (index=1) Then
print(“Factorial of “& cstr(N) & ” is 1″)
else
While index >1
factorial = factorial*index
index=index-1
Wend
print(“Factorial of “& cstr(N) & ” is ” & cstr(factorial))

Find the factors of a given number
Dim N,factors
N=inputbox(“Please enter the name”)
N=Cint(N)
if N<=0 Then
print(“No factors for negative numbers or zero”)
End If
factors = “”
for i=1 to N
if (N mod i) = 0 then
factors = factors & cstr(i) & vbtab
End If
Next
print (“The factors of “& Cstr(N) & ” are ” & factors)

Print prime numbers between a given range of numbers
Dim N1,N2,N,PrimeNums, Flag
N1=inputbox(“Please enter the starting number”)
N2=inputbox(“Please enter the ending number”)
N1= Cint(N1)
N2= Cint(N2)
PrimeNums = “”
for N=N1 to N2
Flag =0
for i=2 to N-1
if (N mod i)  = 0 then
Flag=1
Exit  for
End If
Next
if Flag = 0 then
PrimeNums = PrimeNums & cstr(N) & vbtab
End If
Next
print(PrimeNums)

Swap 2 Numbers without a temporary variable
Dim N1,N2
N1=inputbox(“Please enter the First number”)
N2=inputbox(“Please enter the Second number”)
N1= Cint(N1)
N2= Cint(N2)
print(“Numbers before swapping N1= “& N1 & ” and N2 = “& N2)
N1= N1+N2
N2= N1-N2
N1= N1-N2
print(“Numbers aFTER swapping N1= “& N1 & ” and N2 = “& N2)

Find the length of a given string
Dim string,lengthString
string=inputbox(“Please enter the string”)
lengthString = len(str)
print(“Length of string “& string & ” is ” & Cstr(lengthString))

Find how many alpha characters are present in a string
Dim String, noOfAlphaChars,LengthString,ch
string=inputbox(“Please enter the string”)
lengthString = len(str)
noOfAlphaChars =0
for i=1 to lengthString
ch = mid(string,i,1)
if strComp(ch,”[A-Za-Z]”) = 1 Then
noOfAlphaChars =  noOfAlphaChars+1
End If
Next
Print(“Number of Alpha characters in ” & string & ” is ” & noOfAlphaChars )

Find occurrences of a specific character in a string
Dim String, noOfOccurrences,LengthString,ch,inch
string=inputbox(“Please enter the string”)
inch=inputbox(“Please enter the character”)
lengthString = len(str)
noOfOccurrences=0
for i=1 to lengthString
ch = mid(string,i,1)
if ch =inch Then
noOfOccurrences=  noOfOccurrences+1
End If
Next
Print(“Number of Occurrences of characters in ” & string & ” is ” & noOfOccurrences)

Check whether the string is a PALINDROME
Dim String, revString
string=inputbox(“Please enter the string”)
print(“The given string is ” & string)
revString = StrReverse(string)
if StrComp(String, revString) = 0 Then
print(“The given string is palindrome”)
Else
print(“The given string is not palindrome”)
End If

 

Leave a Reply

Your email address will not be published. Required fields are marked *