Problem: Calculate the GCD of Three, Four, or Five Integers. (Vb.Net ,C #)
Solution:
GCD of Two Numbers:
For C# Code Convert It From
http://www.developerfusion.com/tools/convert/vb-to-csharp/
GCD Of Three Numbers:
For C# Code Convert It From
http://www.developerfusion.com/tools/convert/vb-to-csharp/
Solution:
GCD of Two Numbers:
GCD(a,b)
Algorithm
Step 1: a modulus b = r (remainder) If r Not Equal To Zero i.e i < > 0
Then
Step 2: a: =b and b : = r
Repeat Step 1 Till R become Zero i.e i := 0
Step 3:
GCD is b
Stop
VB Code:
Module Module1
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
End While
GCD = SecondNumber
GCD = SecondNumber
Console.WriteLine(GCD)
Console.ReadLine()
End Sub
End Module
End Sub
End Module
For C# Code Convert It From
http://www.developerfusion.com/tools/convert/vb-to-csharp/
GCD Of Three Numbers:
GCD(a,b,c)=GCD(a,GCD(b,c)) |
VB Code:
Module Module1
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
ThirdNumber = Console.ReadLine()
' For GCD(a,b)
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
Module Module1
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
ThirdNumber = Console.ReadLine()
' For GCD(a,b)
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
End While
GCD = SecondNumber
'Now For GCD(GCD(a,b),c)
FirstNumber = GCD
SecondNumber = ThirdNumber
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
End While
GCD = SecondNumber
Console.WriteLine(GCD) ' GCD Of a,b,c
Console.ReadLine()
End Sub
End Module
GCD = SecondNumber
'Now For GCD(GCD(a,b),c)
FirstNumber = GCD
SecondNumber = ThirdNumber
If FirstNumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = FirstNumber
FirstNumber = Temp
End If
NewMod = FirstNumber Mod SecondNumber
While (NewMod > 0)
FirstNumber = SecondNumber
SecondNumber = NewMod
NewMod = FirstNumber Mod SecondNumber
End While
GCD = SecondNumber
Console.WriteLine(GCD) ' GCD Of a,b,c
Console.ReadLine()
End Sub
End Module
For C# Code Convert It From
http://www.developerfusion.com/tools/convert/vb-to-csharp/
The Code Will Become Easy If Using Function And Can perform two three and four by a little modification.
VB Code:
Module Module1
Public Freinds() As String
Dim COUNT As Integer
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
ThirdNumber = Console.ReadLine()
Dim GCD As Integer
GCD = GetGCD(FirstNumber, SecondNumber)
Console.WriteLine(GCD)
FirstNumber = GCD
SecondNumber = ThirdNumber
GCD = GetGCD(FirstNumber, SecondNumber)
Console.ReadLine()
End Sub
Private Function GetGCD(ByVal Firstnumber As Integer, ByVal SecondNumber As Integer) As Integer
If Firstnumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = Firstnumber
Firstnumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = Firstnumber Mod SecondNumber
While (NewMod > 0)
Firstnumber = SecondNumber
SecondNumber = NewMod
NewMod = Firstnumber Mod SecondNumber
End While
Return SecondNumber
End Function
End Module
Public Freinds() As String
Dim COUNT As Integer
Sub Main()
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Dim ThirdNumber As Integer
FirstNumber = Console.ReadLine()
SecondNumber = Console.ReadLine()
ThirdNumber = Console.ReadLine()
Dim GCD As Integer
GCD = GetGCD(FirstNumber, SecondNumber)
Console.WriteLine(GCD)
FirstNumber = GCD
SecondNumber = ThirdNumber
GCD = GetGCD(FirstNumber, SecondNumber)
Console.ReadLine()
End Sub
Private Function GetGCD(ByVal Firstnumber As Integer, ByVal SecondNumber As Integer) As Integer
If Firstnumber < SecondNumber Then
Dim Temp As Integer
Temp = SecondNumber
SecondNumber = Firstnumber
Firstnumber = Temp
End If
Dim NewMod As Integer
Dim GCD As Integer
NewMod = Firstnumber Mod SecondNumber
While (NewMod > 0)
Firstnumber = SecondNumber
SecondNumber = NewMod
NewMod = Firstnumber Mod SecondNumber
End While
Return SecondNumber
End Function
End Module
No comments:
Post a Comment