Program in visual basic to swap two numbers
Steps to create swapping number program in visual basic 6
- Open visual basic 6.0
Create a new form
Draw four textboxes (two textboxes are used for input the numbers and another two displays the result)
- Draw two buttons (one button is used for display swapping numbers by clicking that numbers.)
Then write the code for the swap numbers button and other code for the clear button.
After that, execute the program.
Source code for visual basic swapping program
Private Sub Command1_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
num1 = Val(Text1.Text)
num2 = Val(Text2.Text)
num3 = swapnumber(num1, num2)
Text3.Text = num1
Text4.Text = num2
End Sub
Public Function swapnumber(ByRef v1 As Integer, ByRef v2 As Integer)
Dim t As Integer
temp = v1
v1 = v2
v2 = temp
End Function
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
How to work swapping numbers program in visual basic 6
Now we will see how to work the source code of the visual basic swapping program. When users enter the number in the first two textboxes then those numbers are stores in num1 and num2 variables. After that executes the ‘swapnumber’ function. In that function, I have used one temporary variable ‘temp’ that are stores v1 variables value and v1 variable stores v2 variables value, and then v2 are stores temp variable value that was stored v1 variables value. And then after execution, the swapped number is store in textbox 3 and 4.
I know this is difficult to understand for some students because this is all our theory. But when you do this program practical then I sure you can defiantly solve this program without any errors. Don’t worry I will give some bonus tips at the end due to that you can easily write a program to swap two numbers in visual basic 6. If you want to read more about swapping numbers then you can also check on Wikipedia about swapping numbers.
Why you need the swapping numbers program
Basically, this program is very important for the students who are in computer fields and those want to learn the programming in visual basic. most of the time this program are asked in exam various types like write a program in vb to perform swapping of two numbers, write a program to swap two numbers in visual basic 6, Swapping Two Numbers in visual basic 6, Swapping Two Numbers Using A Function In Vb 6 etc. but don’t confuse because for all these question have one solution of this program.
This type of program will increase your programming language skills. Due to that you can easily use this logic in any other programming languages like python, java, c#, .net etc. In other languages there is only syntax will be change but logic will be same. So, now you realize that how much helpful of this program.
Now I give you some tips for visual basic swapping program fast. First, create a new form then draw one button and one textbox after that just copy and paste the buttons and textboxes don’t draw that again and again this trick will save your time in the practical exams. After that, create the coding for swap numbers button. For the practice you can directly copy and paste the source code from here.
I hope you have understood the logic of this program if you want to know more information or any queries about how to write a program in visual basic to swap two numbers then comment me in comment section I will try to solve your all queries. If you want to read our previews easy program of string functions in visual basic 6 then you can also read that.
0 Comments