Wednesday, August 26, 2015

Programming Visual Basic ( Work Sheet )

1.Use different types of technique to implement the following:

( a )   Write a program to input integer , N between 1 and 9 and print a line of output consisting of ascending digits from 1 to N. For example, if N=4, produce the line 1234.

Module Module1
  Sub Main()
        Dim a, b As Integer
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = 1 To a
            Console.Write(b)
        Next
        Console.ReadLine()
    End Sub
End Module




Module Module1

    Sub Main()
   
        Dim a, b As Integer
        Dim c As String
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = 1 To a
            c = c + Str(b)
        Next
        Console.WriteLine(Val(c))
        Console.ReadLine()
    End Sub

End Module



Module Module1

    Sub Main()
   
        Dim a, b As Integer
        Console.WriteLine("Enter any integer")
        a = Console.ReadLine()
        Do
            b = b + 1
            Console.Write(b)
 Loop Until b = a    
        Console.ReadLine()
    End Sub

End Module




( b )   Write a program to input integer , N between 1 and 9 and print a line of output consisting of descending digits from N to 1. For example, if N=4, produce the line 4321.

Module Module1

    Sub Main()
        Dim a, b As Integer
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = a To 1 Step -1
            Console.Write(b)
        Next
        Console.ReadLine()
    End Sub

End Module

Module Module1

    Sub Main()
   
        Dim a, b As Integer
        Dim c As String
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = 1 To a
            c = Str(b) + c
        Next
        Console.WriteLine(Val(c))
        Console.ReadLine()
    End Sub

End Module


Module Module1

    Sub Main()
   
        Dim a, b As Integer
        Console.WriteLine("Enter any integer")
        a = Console.ReadLine()
        b = a
        Do

            Console.Write(b)
            b = b - 1
        Loop Until b = 0
     
        Console.ReadLine()
    End Sub

End Module




( c )         Write a program to input integer , N between 1 and 9 and print a line of output consisting of ascending digits from 1 to N followed by descending digits from (N-1) to 1. For example, if N=5, produce the line 123454321.

Module Module1

    Sub Main()
        Dim a, b As Integer
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = 1 To a
            Console.Write(b)
        Next
        For b = (a - 1) To 1 Step -1
            Console.Write(b)
        Next
        Console.ReadLine()
    End Sub
End Module


Module Module1

    Sub Main()
   
        Dim a, b, e As Integer
        Dim c As String
        Console.WriteLine("Enter number from 1-9")
        a = Console.ReadLine()
        For b = 1 To a
            c = c + Str(b)
        Next
        For b = (a - 1) To 1 Step -1
            c = c + Str(b)
        Next
        Console.WriteLine(Val(c))
        Console.ReadLine()
    End Sub

End Module






( d )      Write a program that asks a user to type 5 integers and print the largest value.



Module Module1

    Sub Main()
        Dim a(5), n As Integer
        Console.WriteLine("Enter 5 numbers")
        For n = 1 To 5
            a(n) = Console.ReadLine()
        Next
        For n = 1 To 5
            If a(1) < a(n) Then a(1) = a(n)
        Next
        Console.WriteLine("Greatest=" & a(1))
        Console.ReadLine()
    End Sub

End Module





( e )        Write a program that askes the user to type 10 integers and writes the smallest value.

Module Module1

    Sub Main()
        Dim a(10), n As Integer
        Console.WriteLine("Enter 10 numbers")
        For n = 1 To 10
            a(n) = Console.ReadLine()
        Next
        For n = 1 To 10
            If a(1) > a(n) Then a(1) = a(n)
        Next
        Console.WriteLine("smallest=" & a(1))
        Console.ReadLine()
    End Sub

End Module


( f )          Write a program that asks the user to type an integer N and computes the sum of the cubes from 1 to N.

Module Module1

    Sub Main()
        Dim s, a, counter As Integer
        Console.WriteLine("Enter an integer:")
        a = Console.ReadLine()
        For counter = 1 To a
            s = s + counter ^ 3
        Next
        Console.WriteLine("Sum of the cubes from 1 to " & a & " is = " & s)
        Console.ReadLine()
    End Sub

End Module






( g )     Write a program to calculate the factorial of a given number.


Module Module1

    Sub Main()
        Dim a, counter As Integer
        Dim s As Integer = 1
        Console.WriteLine("Enter any integer:")
        a = Console.ReadLine()
        For counter = 1 To a
            s = s * counter
        Next
        Console.WriteLine("The factorial of given integer is= " & s)
        Console.ReadLine()
    End Sub

End Module






( h)      Write a program that asks the user to type a positive integer. When the user types a negative value the program writs ERROR and asks for another value. When the user types 0, it means that the last value has been typed and the program must write the average of the positive integers. If the number typed values is zero the program writes "NO AVERAGE".


Module Module1

    Sub Main()
        Dim a, s, c As Integer

        Console.WriteLine("Enter positive integer: ")

d:
        a = Console.ReadLine()
        If a > 0 Then
            s = a + s
            c = c + 1
            GoTo d
        ElseIf a < 0 Then
            Console.WriteLine("Error!!.Enter positive number")
            GoTo d
        Else
         
            If s = 0 Then
                Console.WriteLine("no average")
            Else
                Console.WriteLine("Average=" & s / c)
                GoTo u
            End If
        End If

u:
        Console.ReadLine()
    End Sub

End Module


Module Module1

    Sub Main()
        Dim a, b, c As Integer
        Console.WriteLine("Enter positive number")
        Do
            a = Console.ReadLine()
            If a > 0 Then
                b = b + a
                c = c + 1
            ElseIf a < 0 Then
                Console.WriteLine("Error.!!!Enter positive number")
            End If
        Loop Until a = 0
        If b = 0 Then
            Console.WriteLine("No Average")
        Else

            Console.WriteLine("Average of all the number are=" & (b / c))
        End If
        Console.ReadLine()
    End Sub

End Module



( i)        Request the user to type positive numbers, or to stop by typing a number smaller than 1. Print the average.

Module Module1

    Sub Main()
        Dim a, s, c As Integer

        Console.WriteLine("Enter positive integer: ")

d:
        a = Console.ReadLine()
        If a > 0 Then
            s = a + s
            c = c + 1
            GoTo d
        ElseIf a < 1 Then
            Console.WriteLine("stop typing negative number")
            s = a + s
            c = c + 1
            GoTo d
        Else
         
            If s = 0 Then
                Console.WriteLine("no average")
            Else
                Console.WriteLine("Average=" & s / c)
                GoTo u
            End If
        End If

u:
        Console.ReadLine()
    End Sub

End Module


Module Module1

    Sub Main()
        Dim a, b, c As Integer
        Console.WriteLine("Enter positive number")
        Do
            a = Console.ReadLine()
            If a > 0 Then
                b = b + a
                c = c + 1
            ElseIf a < 0 Then
                Console.WriteLine("Stop!!Typing negative numbers")
                b = b + a
                c = c + 1
            End If
        Loop Until a = 0
        If b = 0 Then
            Console.WriteLine("No Average")
        Else

            Console.WriteLine("Average of all the number are=" & (b / c))
        End If
        Console.ReadLine()
    End Sub

End Module



( j )     Request the user to type positive numbers, or type 0 to stop. Show how many numbers were between 100 and 200 (Both included).

Module Module1
    Sub Main()
        Dim a, c As Integer
        Console.WriteLine("Enter positive integer: ")
d:
        a = Console.ReadLine()
        If a > 0 Then
            If a >= 100 And a <= 200 Then
                c = c + 1
            Else
                c = c
            End If
        ElseIf a = 0 Then
            GoTo u
        End If
        GoTo d
  Console.WriteLine("Total numbers between 100 and 200 (both included) is = " & c)
        Console.ReadLine()
    End Sub

End Module


Module Module1

    Sub Main()
        Dim a, c As Integer
        Console.WriteLine("Enter positive number")
        Do
            a = Console.ReadLine()
            If a >= 100 And a <= 200 Then  c = c + 1
        Loop Until a = 0
   Console.WriteLine("Total numbers between 100 and 200 (both included) = " & c)
        Console.ReadLine()
    End Sub

End Module


( k )     The country A has 50M inhabitants, and its population grows 3% per year. The country B has  70M and grows 2% per year. Tell in how many years A will surpass B.

( l )       Write a program to calculate the sum of squares of first n natural numbers  using while loop.

Sub Main()
        Dim a, c As Integer
        Dim b As Integer = 1
        Console.WriteLine("Enter any integer:")
        a = Console.ReadLine()
        Do While b <> (a + 1)
            c = b ^ 2 + c
            b = b + 1
     Loop
        Console.WriteLine(c)
    Console.ReadLine()
    End Sub

End Module






( m )    Write a program to reverse the given number.

Module Module1

    Sub Main()
        Dim a, b, c As String
        Console.WriteLine("Enter any integer")
        a = Console.ReadLine()
        Do Until a = 0
            b = a Mod 10
            a = a \ 10
            c = c * 10 + b
        Loop
        Console.WriteLine(c)
        Console.ReadLine()
    End Sub

End Module


Module Module1
    Sub Main()
        Dim a, c As Integer
        Dim d As String
        Console.WriteLine("Enter any number")
        a = Console.ReadLine()
        d = Str(a)
        c = Val(StrReverse(d))
        Console.WriteLine(c)
        Console.ReadLine()
    End Sub

End Module








( n )     Write a program to check whether the given number is palindrome or not.

Module Module1

    Sub Main()
        Dim a, b, c, d As String
        Console.WriteLine("Enter any intege:r")
        a = Console.ReadLine()
        d = a
        Do Until a = 0
            b = a Mod 10
            a = a \ 10
            c = c * 10 + b
        Loop
        If c = d Then
            Console.WriteLine("The given number is pallindrome!!")
        Else
            Console.WriteLine("The given is not pallindrome!!")
        End If
        Console.ReadLine()
    End Sub

End Module




Module Module1

    Sub Main()
        Dim a, c As Integer
        Dim d As String
        Console.WriteLine("Enter any number")
        a = Console.ReadLine()
        d = Str(a)
        c = Val(StrReverse(d))
        If a = c Then
            Console.WriteLine("Pallindrome!!")
        Else
            Console.WriteLine("Not..Pallindrome!!")
        End If

        Console.ReadLine()
    End Sub

End Module