VB Script General Examples

download VB Script General Examples

of 12

Transcript of VB Script General Examples

  • 7/30/2019 VB Script General Examples

    1/12

    VB Script General Examples I

    1) Write a program for finding out whether the given year is a leap year or not?

    Dim xyear

    xyear=inputbox (Enter Year)

    If xyear mod 4=0 Then

    msgbox This is a Leap year

    Else

    msgbox This is NOT

    End If

    2) Write a program for finding out whether the given number is, Even number or Odd number?

    Dim num

    num=inputbox (Enter a number)

    If num mod 2=0 Then

    msgbox This is a Even Number

    Else

    msgbox This is a Odd Number

    End If

    3) Read two numbers and display the sum?

    Dim num1,num2, sum

    num1=inputbox (Enter num1)

    num2=inputbox (Enter num2)

    sum= Cdbl (num1) + Cdbl (num2) if we want add two strings conversion require

    msgbox (Sum is &sum)

    4) Read P,T,R values and Calculate the Simple Interest?

    Dim p,t, r, si

    p=inputbox (Enter Principle)

    t=inputbox (Enter Time)

    r=inputbox (Enter Rate of Interest)

    si= (p*t*r)/100 p= principle amount, t=time in years, r= rate of interest

  • 7/30/2019 VB Script General Examples

    2/12

    msgbox (Simple Interest is &si)

    5) Read Four digit number, calculate & display the sum of the number or display Error message if the

    number is not a four digit number?

    Dim num, sum

    num=inputbox (Enter a Four digit number)

    If Len(num) = 4 Then

    sum=0

    sum=sum+num mod 10

    num=num/10

    num= left (num, 3)

    sum=sum+num mod 10

    num=num/10

    num= left (num, 2)

    sum=sum+num mod 10

    num=num/10

    num= left (num, 1)

    sum=sum+num mod 10

    msgbox (Sum is &sum)

    else

    msgbox Number, you entered is not a 4 digit number

    End If

    6) Read any Four-digit number and display the number in reverse order?

    Dim num,rev

    num= inputbox(Enter a number)

    If len(num)=4 Then

    rev=rev*10 + num mod 10

    num=num/10

    num= left(num,3)

    rev=rev*10 + num mod 10

  • 7/30/2019 VB Script General Examples

    3/12

    num=num/10

    num= left(num,2)

    rev=rev*10 + num mod 10

    num=num/10

    num= left(num,1)

    rev=rev*10 + num mod 10

    msgbox Reverse Order of the number is &rev

    Else

    msgbox Number, you entered is not a 4 digit number

    End If

    7) Read 4 subjects marks; calculate the Total marks and grade?

    a) If average marks Greater than or equal to 75, grade is Distinction

    b) If average marks Greater than or equal to 60 and less than 75 , then grade

    is First

    c) If average marks Greater than or equal to 50 and less than 60 , then grade

    is Second

    d) If average marks Greater than or equal to 40 and less than 50 , then grade

    is Third

    e) Minimum marks 35 for any subject, otherwise no grade fail)

    Dim e,m,p,c, tot

    e=inputbox (Enter english Marks)

    m=inputbox (Enter maths Marks)

    p=inputbox (Enter physics Marks)

    c=inputbox (Enterchemistry Marks)

    tot= cdbl(e) + cdbl(m) + cdbl(p) + cdbl(c)

    msgbox tot

    If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot

    >=300 Then

    msgbox Grade is Distinction

  • 7/30/2019 VB Script General Examples

    4/12

    else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot

    >=240 and tot=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot

    >=200 and tot=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot

    >=160 and tot

  • 7/30/2019 VB Script General Examples

    5/12

    n= inputbox (Enter any Value)

    num=1

    For num= 1 to n step 1

    Set fso= createobject (scripting.filesystemobject)

    set myfile=fso.opentextfile (E:\Venkat.txt, 8, true)

    myfile.writeline num

    myfile.close

    Next

    11) Display Natural numbers in reverse order up to n?

    Dim num,n

    n=Inputbox (Enter a Vaule)

    For num=n to 1 step -1

    msgbox num

    Next

    12) Display Natural numbers sum up to n? (Using ForNext Loop)

    Dim num, n, sum

    n= inputbox (Enter a Value)

    sum=0

    For num= 1 to n step 1

    sum= sum+num

    Next

    msgbox sum

    13) Display Natural numbers sum up to n? (using WhileWend Loop)

    Dim num, n, sum

    n= inputbox (Enter a Value)

    While num

  • 7/30/2019 VB Script General Examples

    6/12

    Wend

    msgbox sum

    14) Display Natural numbers sum up to n? (Using DoUntilLoop)

    Dim num, n, sum

    n= inputbox (Enter a Value)

    sum=0

    num=1

    Do

    sum= sum+num

    num=num+1

    Loop Until num =cdbl (n+1)

    msgbox sum

    15) Write a Function for Natural Numbers sum up to n?

    Function NNumCou (n)

    Dim num, sum

    sum=0

    For num= 1 to n step 1

    sum= sum+num

    Next

    msgbox sum

    End Function

    16) Verify weather the entered 10 digit value is a numeric value or not?

    Dim a,x,y,z,num

    num=Inputbox (Enter a Phone Number)

    d1= left (num,1)

    d10=Right (num,1)

    d2=mid (num, 2, len (1))

    d3=mid (num, 3, len (1))

    d4=mid (num, 4, len (1))

  • 7/30/2019 VB Script General Examples

    7/12

    d5=mid (num, 5, len (1))

    d6=mid (num, 6, len (1))

    d7=mid (num, 7, len (1))

    d8=mid (num, 8, len (1))

    d9=mid (num, 9, len (1))

    If isnumeric (d1) = True and isnumeric (d2) = True and isnumeric (d3) =

    True and isnumeric (d4) = Trueand isnumeric (d5) = Trueand isnumeric (d6)

    = Trueand isnumeric (d7) = Trueand isnumeric (d8) = Trueand isnumeric (d9)

    = Trueand isnumeric (d10) = True Then

    msgbox It is a Numeric Value

    else

    Msgbox It is NOT Numeric

    End If

    17) Verify weather the entered value is a 10 digit value or not and Numeric value or not? (Using

    multiple if conditions)

    Dim a,x,y,z,num

    num=Inputbox (Enter a Phone Number)

    d1= left (num,1)

    d10=Right (num,1)

    d2=mid (num, 2, len (1))

    d3=mid (num, 3, len (1))

    d4=mid (num, 4, len (1))

    d5=mid (num, 5, len (1))

    d6=mid (num, 6, len (1))

    d7=mid (num, 7, len (1))

    d8=mid (num, 8, len (1))

    d9=mid (num, 9, len (1))

    If len (num) =10 Then

    If isnumeric (d1) = True and isnumeric (d2) = True and isnumeric (d3) =

  • 7/30/2019 VB Script General Examples

    8/12

    True and isnumeric (d4) = Trueand isnumeric (d5) = Trueand isnumeric (d6)

    = Trueand isnumeric (d7) = Trueand isnumeric (d8) = Trueand isnumeric (d9)

    = Trueand isnumeric (d10) = True Then

    msgbox It is a Numeric Value

    End If

    End If

    If len (num) 10 Then

    Msgbox It is NOT valid Number

    End If

    18) Generate interest for 1 to 5 years (for 1 year -7%, 2 years -8%, 3 years-9%, 4 years-10%, 5 years -

    11%)

    Dim amount, duration, intr

    amount=inputbox(enter amount)

    If amount

  • 7/30/2019 VB Script General Examples

    9/12

    msgbox 4 years intrest is: &intr

    else if duration=5 Then

    intr=amount*11/100

    msgbox 5 years intrest is: &intr

    else

    msgbox invalid data

    End If

    End If

    End If

    End If

    End If

    Next

    End If

    VB Script General Examples-II

    1) Read a value and find size of the value

    Dim val: val=Inputbox(Enter value for val: )

    val_length =Len(val)

    msgbox Size of &val& is &val_length

    2) Read a value and find whether the value is numeric or not?

    Dim val: val=Inputbox(Enter value for val: )

    val_type =IsNumeric(val)

    If val_type = true Then

    msgbox val is Numeric

    else

    msgbox val is not Numeric

    End If

    3)Read a value and find whether the value is Date type data or not?

  • 7/30/2019 VB Script General Examples

    10/12

    Dim val: val=Inputbox(Enter value for val: )

    val_type =IsDate(val)

    If val_type = true Then

    msgbox val is Date type data

    else

    msgbox val is not date type

    End If

    4)Read a value and Verify whether the value is 10-digit number or not and

    started with 9 0r 8.

    Then Display it is a valid mobile number

    Dim val,val_Length, val_Numeric, val_Start

    val=Inputbox(Enter value for val: )

    val_Length= Len(val)

    val_Numeric=IsNumeric(val)

    val_Start=Left(val,1)

    If val_Length=10 and val_Numeric and val_Start=9 or val_Start=8 Then

    msgbox val& is a valid mobile number

    Else

    msgbox val& is not a valid mobile number

    End If

    5) Read a mobile number and verify the series

    if it starts with 92478 or 92471 then display it is TataIndicom number

    if it starts with 98490 or 98480 then display it is Airtel number

    Dim val, val_Length,val_Numeric,val_Series,val_Start

    val=Inputbox(Enter value for val: )

    val_Length= Len(val)

    val_Numeric=IsNumeric(val)

    val_Start=Left(val,1)

  • 7/30/2019 VB Script General Examples

    11/12

    val_Series=Left(val,5)

    If val_Numeric=true Then

    If val_Length=10 and val_Start=9 Then

    If val_Series = 92478 or val_Series=92471 Then

    msgbox It is TataIndicom Number

    ElseIf val_Series=98490 or val_Series = 98480 then

    msgbox It is Airtel Number

    End If

    Else

    msgbox val& is not a valid mobile number

    End If

    Else

    msgbox val& is Invalid data

    End If

    6) Read a Value and Verify weather the value is started with Alfa bytes or not?

    (First letter should be Alfa byte)

    Dim val, val_Asc

    val=Inputbox(enter a value)

    val_Asc=Asc(val)

    Msgbox val_Asc

    If val_Asc>=65 and val_Asc=97 and val_Asc

  • 7/30/2019 VB Script General Examples

    12/12

    End If

    7) Read a value and Verify weather the value is Alfa bytes are not?

    Dim str, valAsc, flag,i

    Dim strlen, counter,valsingle

    counter=0

    str=Inputbox(enter a string value)

    strlen= Len(str)

    For i=1 to strlen step 1

    valsingle=Mid(str,i,1)

    valAsc=Asc(valsingle)

    If valAsc>=65 and valAsc=97 and valAsc