Ejercicio
Binario
Objetivo
Cree un programa en Visual Basic que solicite al usuario un número decimal y muestre su equivalente en forma binaria. Debe repetirse hasta que el usuario ingrese la palabra "fin". No debe usar "ToString", sino divisiones sucesivas.
Código
Imports System
Public Class exercise69
Public Shared Sub Main()
Dim answer As String
Dim result As String
Do
Console.Write("Number to convert (or ""end"")? ")
answer = Console.ReadLine()
If answer <> "end" Then
Dim n As Integer = Convert.ToInt32(answer)
result = ""
While n > 1
Dim remainder As Integer = n Mod 2
result = Convert.ToString(remainder) & result
' Cannot convert ExpressionStatementSyntax, System.ArgumentOutOfRangeException: Exception of type 'System.ArgumentOutOfRangeException' was thrown.
' Parameter name: op
' Actual value was DivideAssignmentStatement.
' at ICSharpCode.CodeConverter.Util.VBUtil.GetExpressionOperatorTokenKind(SyntaxKind op)
' at ICSharpCode.CodeConverter.VB.NodesVisitor.MakeAssignmentStatement(AssignmentExpressionSyntax node)
' at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitAssignmentExpression(AssignmentExpressionSyntax node)
' at Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
' at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
' at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)
' at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitAssignmentExpression(AssignmentExpressionSyntax node)
' at Microsoft.CodeAnalysis.CSharp.Syntax.AssignmentExpressionSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
' at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.ConvertSingleExpression(ExpressionSyntax node)
' at ICSharpCode.CodeConverter.VB.MethodBodyVisitor.VisitExpressionStatement(ExpressionStatementSyntax node)
' at Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
' at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node)
' at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
'
' Input:
' n /= 2;
'
End While
result = Convert.ToString(n) & result
Console.WriteLine("Binary: {0}", result)
End If
Loop While answer <> "end"
End Sub
End Class