×

VB.Net Programs

VB.Net program to find the largest number among three numbers using conditional operator

By Nidhi Last Updated : November 15, 2024

Finding the largest number among three numbers using conditional operator

Here, we will read three integers and check the largest number and print that on the console screen.

VB.Net code to find the largest number among three numbers using conditional operator

The source code to find the largest number between the two numbers is given below. The given program is compiled and executed successfully.

'VB.Net program to find the largest number 
'among three numbers.

Module Module1

    Sub Main()

        Dim result As Integer = 0
        Dim num1 As Integer = 0
        Dim num2 As Integer = 0
        Dim num3 As Integer = 0

        Console.Write("Enter number1: ")
        num1 = Integer.Parse(Console.ReadLine())

        Console.Write("Enter number2: ")
        num2 = Integer.Parse(Console.ReadLine())

        Console.Write("Enter number3: ")
        num3 = Integer.Parse(Console.ReadLine())

        result = If(
                    (num1 > num2 AndAlso num1 > num3), num1, If((num2 > num1 AndAlso num2 > num3), num2, num3)
                   )
        Console.WriteLine("Largest Number is: {0}", result)

        Console.ReadLine()
    End Sub

End Module

Output:

Enter number1: 10
Enter number2: 30
Enter number3: 20
Largest Number is: 30

Explanation:

In the above program, we created a module Module1 that contains Main() method. In the Main() method we created four local variables num1, num2, num3, and result that are initialized with 0. After that we read the values of num1, num2 and num3.

result = If((num1 > num2 AndAlso num1 > num3), num1, 
	     If((num2 > num1 AndAlso num2 > num3), num2, num3)
	    )

Here, we used a nested conditional operator to find the largest number among all the numbers and print the result on the console screen.

VB.Net Basic Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.