Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Semana 8 Repetir Mientras
Semana 8 Repetir Mientras
PROGRAMACIÓN I
ESTRUCTURA MIENTRAS‐HACER
Definición
Ø Esta es una estructura que repetirá un proceso durante “N” veces, donde “N”
puede ser fijo o variable. Para esto, la instrucción se vale de una condición que
es la que debe cumplirse para que se siga ejecutando. Cuando la condición ya
no se cumple, entonces ya no se ejecuta el proceso.
Característica
SINTAXIS
PSEUDOCÓDIGO
VE
VER
RD
DA
AD
D
:
:
FIN‐MIENTRAS In
Ins
sttrru
uc
cc
ciio
on
neess y/
y/o
o
Se
Sen ntte
en
ncciia
as
s
Ejercicios Propuestos
1. Calcule y visualice la suma de los todos los números impares de 3 cifras.
Pseudocódigo
ALGORITMO PROB1
VARIABLES
ENTERO: NUM, SUMA
INICIO
SUMA=0
NUM=101
MIENTRAS (NUM<=999) HACER
SUMA=SUMA + NUM
ESCRIBIR (NUM)
NUM=NUM+2
FIN_MIENTRAS
ESCRIBIR (SUMA)
FIN
Imports System.Console
Module Module1
Sub Main()
Dim num, suma As Integer
num = 101
suma = 0
While (num <= 999)
suma = suma + num
Write(num &" ")
num = num + 2
EndWhile
WriteLine("")
WriteLine("La suma es: "& suma)
Read()
End Sub
End Module
S ← 2 + 4 + 6 + 8 +… + 98
Pseudocódigo
ALGORITMO PROB2
VARIABLES
ENTERO: SUMA, X
INICIO
X=2
SUMA=0
MIENTRAS(X<=98) HACER
SUMA=SUMA+X
SI(X<98) ENTONCES
ESCRIBIR(X,"+")
SINO
ESCRIBIR(X)
FIN_SI
X=X+2
FIN_MIENTRAS
ESCRIBIR (SUMA)
FIN
Imports System.Console
Module Module2
Sub main()
Dim x, suma As Integer
suma = 0
x=2
While (x <= 98)
suma = suma + x
If (x < 98) Then
Write(x &"+")
Else
Write(x)
End If
x=x+2
End While
WriteLine()
WriteLine("La suma es: "& suma)
Read()
End Sub
End Module
S ← 1+2+3/2+4/3+5/4+…+10/9
Pseudocódigo
ALGORITMO PROB03
VARIABLES
ENTERO: X, D
REAL: SUM
INICIO
X=1
SUMA=0
MIENTRAS(X<=10) HACER
SI(X<=2) ENTONCES
SUM=SUM+X
ESCRIBIR(X,"+")
SINOSI(X>2 AND X<=9) ENTONCES
D=X‐1
SUMA=SUMA+ (X/D)
ESCRIBIR(X,"/", D,"+")
SINO
D=X‐1
SUMA=SUMA+ (X/D)
ESCRIBIR(X,"/", D)
FIN_SI
X=X+1
FIN_MIENTRAS
ESCRIBIR (SUMA)
FIN
Imports System.Console
Module Module3
Sub main()
Dim x, d As Integer
Dim suma As Double
x = 1 : suma = 0
While (x <= 10)
If (x <= 2) Then
suma = suma + x
Write(x &"+")
ElseIf (x > 2 And x <= 9) Then
d=x1
suma = suma + (x / d)
Write(x &"/"& d &"+")
Else
d=x1
suma = suma + (x / d)
Write(x &"/"& d)
End If
x=x+1
End While
WriteLine()
WriteLine("La suma de la Serie es "& suma)
Read()
End Sub
End Module
Pseudocódigo
ALGORITMO PROB04
VARIABLES
ENTERO: NUM, SUM, R
INICIO
LEER (NUM)
SUM=0
MIENTRAS (NUM<>0) HACER
R=NUM MOD 10
SUM=SUM+R
NUM=NUM\10
FIN_MIENTRAS
ESCRIBIR (SUM)
FIN
Imports System.Console
Module Module4
Sub main()
Dim num, sum, r As Integer
Write("Ingrese le numero: ") : num = ReadLine()
sum = 0
While (num <> 0)
r = num Mod 10
sum = sum + r
num = num \ 10
EndWhile
Write("La suma es: "& sum)
Read()
End Sub
End Module
5. Ingrese un número entero y luego calcule la suma de cifras pares e impares que
están contenidas en el número ingresado.
Pseudocódigo
ALGORITMO PROB05
VARIABLES
ENTERO: NUM, SUMPAR, SUMIMPAR
INICIO
LEER (NUM)
SUMPAR=0
SUMIMPAR=0
MIENTRAS (NUM<>0) HACER
R=NUM MOD 10
SI(R MOD 2=0) ENTONCES
SUMPAR=SUMPAR+R
SINO
SUMIMPAR=SUMIMPAR+R
FIN_SI
NUM=NUM\10
FIN_MIENTRAS
ESCRIBIR (SUMPAR, SUMIMPAR)
FIN
Imports System.Console
Module Module5
Sub main()
Dim num, sumpar, sumimpar, r As Integer
Write("Ingrese un numero: ") : num = ReadLine()
sumpar = 0 : sumimpar = 0
While (num <> 0)
r = num Mod 10
If (r Mod 2 = 0) Then
sumpar = sumpar + r
Else
sumimpar = sumimpar + r
End If
num = num \ 10
End While
WriteLine("La suma de los impares son: "& sumimpar)
WriteLine("La suma de los pares son: "& sumpar)
Read()
End Sub
End Module
Pseudocódigo
ALGORITMO PROB06
VARIABLES
ENTERO: NUM, MAX, MIN, R
INICIO
LEER (NUM)
MAX=0
MIN=9
MIENTRAS (NUM<>0) HACER
R=NUM MOD 10
SI(R>MAX) ENTONCES
MAX=R
FIN_SI
SI(R<MIN) ENTONCES
MIN=R
FIN_SI
NUM=NUM\10
FIN_MIENTRAS
ESCRIBIR (MAX, MIN)
FIN
Imports System.Console
Module Module6
Sub main()
Dim num, r, max, min As Integer
Write("Ingrese un numero: ") : num = ReadLine()
max = 0
min = 9
While (num <> 0)
r = num Mod 10
If (r > max) Then
max = r
End If
If (r < min) Then
min = r
End If
num = num \ 10
End While
WriteLine("La mayor cifra es: "& max)
WriteLine("La menor cifra es: "& min)
Read()
End Sub
End Module
7. Ingrese un número entero y luego forme un 2do número con las cifras del
número ingresado en forma inversa.
Pseudocódigo
ALGORITMO PROB07
VARIABLES
ENTERO: N, R, INV
INICIO
LEER(N)
INV=0
MIENTRAS(N<>0) HACER
R=N MOD 10
INV=INV*10+R
N=N\10
FIN_MIENTRAS
ESCRIBIR (INV)
FIN
Imports System.Console
Module Module7
Sub main()
Dim n, r, inv As Integer
Write("Ingrese un numero: ") : n = ReadLine()
inv = 0
While (n <> 0)
r = n Mod 10
inv = inv * 10 + r
n = n \ 10
End While
WriteLine("El Numero Invertido es : "& inv)
Read()
End Sub
End Module
Pseudocódigo
ALGORITMO PROB08
VARIABLES
ENTERO: N, R
CADENA: CAD
INICIO
LEER(N)
CAD=""
MIENTRAS(N<>0) HACER
R=N MOD 5
CAD=R + CAD
N=N\5
FIN_MIENTRAS
ESCRIBIR (CAD)
FIN
Imports System.Console
Module Module8
Sub main()
Dim n, r As Integer
Dim cad As String
Write("Ingrese un Numero: ") : n = ReadLine()
cad = ""
While (n <> 0)
r = n Mod 5
cad = r & cad
n=n\5
End While
WriteLine("El numero en base 5 es : "& cad)
Read()
End Sub
End Module
Pseudocódigo
ALGORITMO PROB09
VARIABLES
CADENA: NOM
ENTERO: ED, SUM, MAX, C
REAL: PROM
INICIO
SUM=0
MAX=0
PROM=0
C=1
MIENTRAS(C<=5) HACER
LEER (NOM, ED)
SI(ED>17 Y ED<=70) ENTONCES
SUM=SUM+ED
SI(ED>MAX) ENTONCES
MAX=ED
FIN_SI
C=C+1
FIN_SI
FIN_MIENTRAS
PROM=SUM/5
ESCRIBIR (SUM, PROM)
FIN
Imports System.Console
Module Module9
Sub main()
Dim nom AsString
Dim ed, sum, max, c As Integer
Dim prom As Double
sum = 0 : max = 0 : prom = 0
c=1
While (c <= 5)
Write("Escriba el nombre "& c &": ") : nom = ReadLine()
Write("Escriba la edad "& c &": ") : ed = ReadLine()
If (ed > 17 And ed <= 70) Then
sum = sum + ed
If (ed > max) Then
max = ed
End If
c=c+1
End If
End While
prom = sum / 5
WriteLine("La suma de Edades es: "& sum)
WriteLine("El promedio es: "& prom)
Read()
End Sub
End Module
ALGORITMO PROB10
VARIABLES
ENTERO:F,C
INICIO
F=1
MIENTRAS (F<=7) HACER
C=1
MIENTRAS(C<=7) HACER
ESCRIBIR ("%")
C=C+1
FIN_MIENTRAS
SALTO DE LINEA
F=F+1
FIN_MIENTRAS
FIN
Imports System.Console
Module Module10
Sub main()
Dim f, c As Integer
f=1
While (f <= 7)
c=1
While (c <= f)
Write("%")
c=c+1
End While
WriteLine()
f=f+1
End While
Read()
End Sub
End Module
Imports System.Console
Module Module11
Sub main()
Dim n, f, p, e As Integer
Write("Escriba el numero: ") : n = ReadLine()
f=1
While (f <= n)
e=1
While (e <= n f)
Write(" ")
e=e+1
End While
p=1
While (p <= f)
Write("* ")
p=p+1
End While
WriteLine()
f=f+1
End While
Read()
End Sub
End Module
ALGORITMO PROB12
VARIABLES
ENTERO: N, CONT, D
REAL: SUM
INICIO
LEER(N)
CONT=1
MIENTRAS (CONT<=N)
SI (CONT=1) ENTONCES
SUM=1
ESCRIBIR ("1")
SINO
D=20*CONT‐8
SUM=SUM+(1/D)
ESCRIBIR ("+1/", D)
FIN_SI
CONT=CONT+1
FIN_MIENTRAS
ESCRIBIR (SUM)
FIN
Imports System.Console
Module Module12
Sub main()
Dim n, cont, d As Integer
Dim sum As Double
Write("Escriba el numero: ") : n = ReadLine()
cont = 1
While (cont <= n)
If (cont = 1) Then
sum = 1
Write("1")
Else
d = 20 * cont 8
sum = sum + (1 / d)
Write("+1/"& d)
End If
cont = cont + 1
End While
WriteLine()
Write("La suma: "& sum)
Read()
End Sub
End Module
13. Calcular la suma de todos los números primos existentes entre 1 y 1000.
Pseudocódigo
ALGORITMO PROB13
VARIABLES
ENTERO: N, C, X, F
INICIO
F=1
MIENTRAS(N<=1000) HACER
X=1
C=0
MIENTRAS(X<=N) HACER
SI(N MOD X=0) ENTONCES
C=C+1
FIN_SI
X=X+1
FIN_MIENTRAS
SI(C=2) ENTONCES
ESCRIBIR(N)
FIN_SI
N=N+1
FIN_MIENTRAS
FIN
Imports System.Console
Module Module13
Sub main()
Dim n, c, x, f As Integer
F=1
While (n <= 1000)
x=1
c=0
While (x <= n)
If (n Mod x = 0) Then
c=c+1
End If
x=x+1
End While
If (c = 2) Then
Write(n &" ")
End If
n=n+1
End While
Read()
End Sub
End Module
14. Calcular la suma de todos los números de 3 cifras compuestos por cifras
diferentes impares.
Pseudocódigo
ALGORITMO PROB14
VARIABLES
ENTERO: U, D, C, X, SUMA
INICIO
X=100
SUMA=0
MIENTRAS(X<=999) HACER
C=X\100
D=(X MOD 100)\10
U=X MOD 10
SI(C Mod 2=0 Y D Mod 2=0 Y U Mod 2=0) ENTONCES
SI(C<>D Y C<>U Y D<>U) ENTONCES
SUMA=SUMA+X
ESCRIBIR(X)
FIN_SI
FIN_SI
X=X+1
FIN_MIENTRAS
ESCRIBIR (SUMA)
FIN
Imports System.Console
Module Module14
Sub main()
Dim u, d, c, x, suma As Integer
x = 100
suma = 0
While (x <= 999)
c = x \ 100
d = (x Mod 100) \ 10
u = x Mod 10
If (c Mod 2=0 And d Mod 2=0 And u Mod 2=0) Then
If (c <> d And c <> u And d <> u) Then
suma = suma + x
Write(x &" ")
End If
End If
x=x+1
End While
WriteLine()
WriteLine("La suma es "& suma)
Read()
End Sub
End Module
Imports System.Console
Module Module15
Sub main()
Dim x, n As Integer
Write("Escriba el numero: ") : n = ReadLine()
x=1
While (x <= n)
If (x Mod 2 = 0) Then
Write("+"& x)
Else
Write(""& x)
End If
x=x+1
End While
Read()
End Sub
End Module