0% found this document useful (0 votes)
3 views13 pages

Algoritmos de Cálculo en Visual Basic

This document contains Visual Basic code snippets for performing various mathematical calculations and operations on numbers, including summing the first n natural numbers, calculating the factorial of a number, counting the number of even numbers between two intervals, determining the number of even/odd digits in a number, finding the largest/smallest digit in a number, checking if a number is prime, and sorting lists of numbers. The code uses loops and conditionals to iterate through numbers and compare/manipulate values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Algoritmos de Cálculo en Visual Basic

This document contains Visual Basic code snippets for performing various mathematical calculations and operations on numbers, including summing the first n natural numbers, calculating the factorial of a number, counting the number of even numbers between two intervals, determining the number of even/odd digits in a number, finding the largest/smallest digit in a number, checking if a number is prime, and sorting lists of numbers. The code uses loops and conditionals to iterate through numbers and compare/manipulate values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

'suma de los primeros n numeros naturales

Dim n, i, s As Integer

n = [Link]
i = 1
'usamos el bucle
Do While i <= n
s = s + i
i = i + 1
Loop
[Link] = s

'factorial de un numero
Dim n, i, f As Integer
n = [Link]
i = 1
f = 1
'usamos el bucle
Do While i <= n
f = f * i

i = i + 1
Loop
[Link] = f

'cantidad de numeros pares en un intervalo


Dim ni, nf, i, c As Integer

ni = [Link]
nf = [Link]
i = ni + 1

Do While i < nf
If i Mod 2 = 0 Then
c = c + 1
End If
i = i + 1

Loop

[Link] = c

'dado un numero determinar la cantidad de digitos pares


Dim n, c, d As Integer

n = [Link]

Do While n >= 1
d = n Mod 10

If d Mod 2 = 0 Then
c = c + 1
End If

n = n \ 10

Loop

[Link] = c

'dado un numero determinar la cantidad de digitos


Dim n, c, As Long

n = [Link]

Do While n >= 1
c = c + 1

n = n \ 10

Loop

[Link] = c
'dado un numero devolver el mayor digito
Dim n, mayor, d As Integer

n = [Link]

Do While n >= 1
d = n Mod 10
If d > mayor Then
mayor = d

End If
n = n \ 10

Loop
[Link] = mayor
'dado un numero devolver el numero invertido

Dim n, d, c As Integer

n = [Link]

Do While n >= 1
d = n Mod 10
c = c * 10 + d

n = n \ 10

Loop
[Link] = c
'determoinar si un numero es primo

Dim n, i, c As Integer
Dim r As String

n = [Link]
i = 1
Do While i <= n
If (n Mod i) = 0 Then
c = c + 1
End If

i = i + 1

Loop
If c = 2 Or n = 1 Then
r = "es primo"
Else
r = "no es primo"
End If
[Link] = r

'ordenar 3 numeros ascendentemente


Dim n1, n2, n3, aux As Integer

n1 = [Link]
n2 = [Link]
n3 = [Link]
If n1 > n2 Then
n1 = aux
n1 = n2
n2 = aux
End If
If n1 > n3 Then
n1 = aux
n1 = n3
n3 = aux

End If
If n2 > n3 Then
n2 = aux
n2 = n3
n3 = aux

End If

[Link] = n1
[Link] = n2
[Link] = n3
'ordenar 4 numeros
Dim n1, n2, n3, aux, n4 As Integer

n1 = [Link]
n2 = [Link]
n3 = [Link]
n4 = [Link]

If n1 > n2 Then
aux = n1
n1 = n2
n2 = aux

End If
If n1 > n3 Then
aux = n1
n1 = n3
n3 = aux

End If
If n1 > n4 Then
aux = n1
n1 = n4
n4 = aux

End If
If n2 > n3 Then
aux = n2
n2 = n3
n3 = aux

End If
If n2 > n4 Then
aux = n2
n2 = n4
n4 = aux

End If
If n3 > n4 Then
aux = n3
n3 = n4
n4 = aux

End If
[Link] = n1
[Link] = n2
[Link] = n3
[Link] = n4
'ordenar 5 numeros
Dim n1, n2, n3, aux, n4, n5 As Integer

n1 = [Link]
n2 = [Link]
n3 = [Link]
n4 = [Link]
n5 = [Link]

If n1 > n2 Then
aux = n1
n1 = n2
n2 = aux

End If
If n1 > n3 Then
aux = n1
n1 = n3
n3 = aux

End If
If n1 > n4 Then
aux = n1
n1 = n4
n4 = aux

End If
If n1 > n5 Then
aux = n1
n1 = n5
n5 = aux

End If

If n2 > n3 Then
aux = n2
n2 = n3
n3 = aux

End If
If n2 > n4 Then
aux = n2
n2 = n4
n4 = aux

End If
If n2 > n5 Then
aux = n2
n2 = n5
n5 = aux

End If
If n3 > n4 Then
aux = n3
n3 = n4
n4 = aux

End If
If n3 > n5 Then
aux = n3
n3 = n5
n5 = aux
End If
If n4 > n5 Then
aux = n4
n4 = n5
n5 = aux

End If

[Link] = n1
[Link] = n2
[Link] = n3
[Link] = n4
[Link] = n5

You might also like