Function calculator, params and return value of Main VB.Net Exercise - Visual Basic Programming Course


 Lesson

Functions

 Exercise

Function calculator, params and return value of Main

Objetive

Write a Visual Basic (VB.Net) program to calculate a sum, subtraction, product or division, analyzing the command line parameters:

calc 5 + 379

(Parameters must be a number, a sign, and another number; allowed signs are + - * x / )

This version must return the following error codes:
1 if the number of parameters is not 3
2 if the second parameter is not an accepted sign
3 if the first or third parameter is not a valid number
0 otherwise

Code

Imports System
Public Class exercise126
    Public Shared Function Main(ByVal args As String()) As Integer
        If args.Length <> 3 Then
            Console.WriteLine("Error!")
            Console.WriteLine("Usage: number1 operand number2")
            Console.WriteLine("Where operand can be + - / * x ·")
            Return 1
        End If

        Try
            Dim number1 As Integer = Convert.ToInt32(args(0))
            Dim number2 As Integer = Convert.ToInt32(args(2))

            Select Case args(1)
                Case "+"
                    Console.WriteLine(number1 + number2)
                    Exit Select
                Case "-"
                    Console.WriteLine(number1 - number2)
                    Exit Select
                Case "/"
                    Console.WriteLine(number1 / number2)
                    Exit Select
                Case "*", "x", "·"
                    Console.WriteLine(number1 * number2)
                    Exit Select
                Case Else
                    Console.WriteLine("Error!")
                    Console.WriteLine("Operand must be + - / * x or ·")
                    Return 2
                    Exit Select
            End Select

        Catch __unusedException1__ As Exception
            Console.WriteLine("Error!")
            Console.WriteLine("Not a valid number")
            Return 3
        End Try

        Return 0
    End Function
End Class

More VB.Net Exercises of Functions

 Functions: greeting + farewell
Write a Visual Basic (VB.Net) program whose Main must be like this: public static void Main() { SayHello(); SayGoodbye(); } SayHello and Say...
 Function with parameters
Write a Visual Basic (VB.Net) program whose Main must be like this: public static void Main() { SayHello("John"); SayGoodbye(); } SayHello a...
 Function returning a value
Write a Visual Basic (VB.Net) program whose Main must be like this: public static void Main() { int x= 3; int y = 5; Console.WriteLine( Sum(x,y...
 Function returning a value V2
Write a Visual Basic (VB.Net) program whose Main must be like this: public static void Main() { __Console.WriteLine("\"Hello, how are you\" conta...
 Function write centered
Write a Visual Basic (VB.Net) function to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 character...
 Function write underlined
Write a Visual Basic (VB.Net) function able to write centered on screen the text that is indicated as a parameter (supposing a screen width of 80 char...
 Function sum of array
Write a Visual Basic (VB.Net) program to calculate the sum of the elements in an array. "Main" should be like this: public static void Main() { in...
 Function double
Write a Visual Basic (VB.Net) function named "Double" to calculate and return an integer number doubled. For example. Double(7) should return 14....
 Function Double reference parameter
Write a Visual Basic (VB.Net) function named "Double" to calculate the double of an integer number, and modify the data passed as an argument. It must...
 Function swap reference parameters
Write a Visual Basic (VB.Net) function named "Swap" to swap the values of two integer numbers, which are passed by reference. An example of use mig...
 Function power local variables
Write a Visual Basic (VB.Net) function named "Power" to calculate the result of raising an integer number to another (positive integer) number. It mus...
 Function recursive power
Write a Visual Basic (VB.Net) function that calculates the result of raising an integer to another integer (eg 5 raised to 3 = 53 = 5 × 5 × 5 = 125). ...
 Function Fibonacci
Write a Visual Basic (VB.Net) program that uses recursion to calculate a number in the Fibonacci series (in which the first two items are 1, and for t...
 Function modify a letter in a string
Write a Visual Basic (VB.Net) function named "ChangeChar" to modify a letter in a certain position (0 based) of a string, replacing it with a differen...
 Function IsPrimeTarea
Write a Visual Basic (VB.Net) function named "IsPrime", which receives an integer number and retuns true if it is prime, or false if it is not: if ...
 Function Parameters of Main, Sum
Write a Visual Basic (VB.Net) program named "sum", which receives two integer numbers in the command line and displays their sum, as in this example: ...
 Function SumDigits
Write a Visual Basic (VB.Net) function SumDigits that receives a number and returns any results in the sum of its digits. For example, if the number i...
 Function Factorial
The factorial of a number is expressed as follows: n! = n · (n-1) · (n-2) · (n-3) · ... · 3 · 2 · 1 For example, 6! = 6·5·4·3·2·1 Create a r...
 Function Parameters of Main, Reverse
Write a Visual Basic (VB.Net) program named "reverse", which receives several words in the command line and displays them in reverse order, as in this...
 Function GetInt
Write a Visual Basic (VB.Net) function named "GetInt", which displays on screen the text received as a parameter, asks the user for an integer number,...
 Function tasks database
Write in Visual Basic (VB.Net) an improved version of the "tasks database", splitting it into functions....
 Function Greatest value in a array
Write a Visual Basic (VB.Net) function which returns the greatest value stored in an array of real numbers which is specified as parameter: float[]...
 Function factorial (iterative)
Write an Visual Basic (VB.Net) iterative (non-recursive) function to calculate the factorial of the number specified as parameter: Console.Write ( ...
 Function WriteTitle
Write a Visual Basic (VB.Net) function named "WriteTitle" to write a text centered on screen, uppercase, with extra spaces and with a line over it and...
 Function return value for Main
Write a Visual Basic (VB.Net) program in which you write a title (using the previous WriteTitle function) which the user will specify in command line....
 Function CountDV
Write a Visual Basic (VB.Net) function that calculates the amount of numeric digits and vowels that a text string contains. It will accept three param...
 Function IsAlphabetic
Write a Visual Basic (VB.Net) function that tells if a character is alphabetic (A through Z) or not. It should be used like this: if (IsAlphabetic ...
 Function IsNumber
Write a Visual Basic (VB.Net) function that tells if a string is an intenger number. It should be used like this: if (IsNumber ("1234")) System.Co...
 Function calculator, params of Main
Write a Visual Basic (VB.Net) program to calculate a sum, subtraction, product or division, analyzing the command line parameters: calc 5 + 379 ...
 Function MinMaxArray
Write a Visual Basic (VB.Net) function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters: ...
 Function Reverse, recursive
Write a Visual Basic (VB.Net) program that uses recursion to reverse a string of characters (for example, from "Hello" it would return "olleH")....
 Function WriteRectangle
Write a Visual Basic (VB.Net) function WriteRectangle to display a (filled) rectangle on the screen, with the width and height indicated as parameters...
 Function Palindrome, iterative
Write an Visual Basic (VB.Net) iterative function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
 Function Palindrome, recursive
Write a Visual Basic (VB.Net) recursive function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome....
 Function GetMinMax
Write a Visual Basic (VB.Net) function named "GetMinMax", which will ask the user for a minimum value (a number) and a maximum value (another number)....
 Function Multiply & MultiplyR
Write two Visual Basic (VB.Net) functions, Multiply and MultiplyR, to calculate the product of two numbers using sums. T he first version must be iter...


Juan A. Ripoll - Systems Tutorials and Programming Courses © 2024 All rights reserved.  Legal Conditions.