Home >>VB.Net Programs >VB.Net program to input and print an integer variable

VB.Net program to input and print an integer variable

Write a VB.Net program to input and print an integer variable

We'll create a program that will read an integer variable from the user and print it on the screen of the console.

Below is the source code to input and print an integer variable. The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim p As Integer = 0

        Console.Write("Enter an integer value: ")
        p = Convert.ToInt32(Console.ReadLine())

        'print the value
        Console.WriteLine("p = {0}", p)
        Console.ReadLine()
    End Sub
End Module
	
Output:
Enter an integer value: 10
p = 10

Explanation:

We created a Module in the above program that contains the Main() process, here we created a local variable initialised with 0.

Console.Write("Enter an integer value: ")
num = Convert.ToInt32(Console.ReadLine())
 

Here, we read the value using the ReadLine() method, but the ReadLine() method returns the string, so we need to convert it to an integer using the ToInt32() Convert class method, and then assign the value to the num variable. After that the a value was printed on the console screen.


No Sidebar ads