You are on page 1of 3

Soluciones AP 5 Unidad 4

a. Calcular la suma de N nmeros naturales.


Algoritmo Suma_n_naturales
Var
N,I,Numero,Sumatoria: Entero
Inicio
(t1) Leer N
(t2) Sumatoria 0
(t4) Para ((t3)I=1;I<=N;(t7) I+1) hacer
(t5) Leer Numero
(t6) Sumatoria
Sumatoria+Numero
Finpara
(t8)Escribir Sumatoria
Fin

Accin
ejecutada
Inicial
t1
t2
t3
t4 cont.
t5
t6
t7
t4 cont.
t5
t6
t7
t4 cont.
t5
t6
t7
t4 fin
t8

Estado del Ambiente despues de


ejecutada la accin
Salida
Numero
N
I
Sumatoria
Indet. Indet. Indet.
Indet.
Indet.
3
Indet.
Indet.
Indet.
3
Indet.
0
Indet.
3
1
0
Indet.
3
1
0
a
3
1
0
a
3
1
a
a
3
2
a
a
3
2
a
b
3
2
a
b
3
2
a+b
b
3
3
a+b
b
3
3
a+b
c
3
3
a+b
c
3
3
a+b+c
c
3
4
a+b+c
c
3
4
a+b+c
c
3
4
a+b+c
a+b+c

b. Calcule el producto de dos nmeros naturales mediante sumas sucesivas.

Algoritmo producto_numeros
Var
N1, N2, Producto, I :Entero
Inicio
(t1.1) Producto 0
(t1.2) I 1
(t2.1) Leer N1
(t2.2) Leer N2
(t3) Mientras (I <= N2) hacer
(t4) Producto producto+N1
(t5) I I+1
Finmientras
(t6) Escribir Producto=producto
Fin

Accin
ejecutada
Inicial
t1.1
t1.2
t2.1
t2.2
t3 cont
t4
t5
t3 cont.
t4
t5
t3 cont.
t4
t5
t3 fin
t6

Estado del Ambiente despues de


ejecutada la accin
N1
Indet.
Indet.
Indet.
a
a
a
a
a
a
a
a
a
a
a
a
a

N2
Indet.
Indet.
Indet.
Indet.
3
3
3
3
3
3
3
3
3
3
3
3

I
Indet.
Indet.
1
1
1
1
1
2
2
2
3
3
3
4
4
4

Salida

Producto
Indet.
0
0
0
0
0
a
a
a
a+a
a+a
a+a
a+a+a
a+a+a
a+a+a
a+a+a a+a+a

c. Calcule el cociente entero y el resto de dividir dos nmeros naturales a y b, con a mayor o igual que b,
mediante restas sucesivas.
Algoritmo cociente_resto_numeros
Var
a, b, cociente :Entero
Inicio
(t1.1) Leer a
(t1.2) Leer b
(t2) Cociente 0
(t3) Si (a < b) entonces
(t4.1) Escribir divisor mayor que
dividendo
Sino
(t4.2) Mientras (b<=a) hacer
(t5.1)a a-b
(t5.2)cociente cociente+1
Finmientras
(t6.1) Escribir Cociente=cociente
(t6.2) Escribir Resto= a
Finsi
Fin

Accin
ejecutada

Estado del Ambiente despues


de ejecutada la accin

Salida

Inicial
t1.1
t1.2
t2
t3->t4.1
t4.1 fin

a
Indet.
5
5
5
5
5

b
Indet.
Indet.
7
7
7
7

cociente
Indet.
Indet.
Indet.
0
0
0

dvsor>dvdo

Inicial
t1.1-t1.2
t2
t3->t4.2
t4.2 cont
t5.1-t5.2
t4.2 cont
t5.1-t5.2
t4.2 cont
t5.1-t5.2
t4.2 fin
t6.1
t6.2

Indet.
17
17
17
17
12
12
7
7
2
2
2
2

Indet.
5
5
5
5
5
5
5
5
5
5
5
5

Indet.
Indet.
0
0
0
1
1
2
2
3
3
3
3

Cte=3
Rto=2

d. Compruebe si un nmero es par.


Algoritmo par_impar
Var
N : Entero
Inicio
(t1) Leer N
Hacer
(t2) N N-2
(t3) Mientras (2 <= N)
(t4)Si (N=0) entonces
(t5.1)Escribir Nmero par
Sino
(t5.2)Escribir Nmero Impar
Finsi
Fin

Estado del Ambiente despues


de ejecutada la accin
N
Inicial
Indet.
t1
5
t2
3
t3->hacer
3
t2
1
t3->t4
1
t4
1
t5.2
1
Inicial
Indet.
t1
4
t2
2
t3->hacer
2
t2
0
t3->t4
0
t4
0
t5.1
0

Accin
ejecutada

Salida

N Impar

N Par

e. Calcule la suma de los primeros n nmeros pares.

Algoritmo suma_n_pares
Var
N, suma, I :Entero
Inicio
(t1)Leer N
(t2.1)Suma 0
(t2.2)I 1
(t3) Mientras (I < = N) hacer
(t4.1) suma suma+2*I
(t4.2) I I+1
Finmientras
(t5) Escribir Suma=,suma
Fin

Accin
ejecutada
Inicial
t1
t2.1-t2.2
t3 cont
t4.1-t4.2
t3 cont
t4.1-t4.2
t3 cont
t4.1-t4.2
t3 cont
t4.1-t4.2
t3 fin
t5

Estado del Ambiente despues


de ejecutada la accin
N
Indet.
4
4
4
4
4
4
4
4
4
4
4
4

suma
Indet.
Indet.
0
0
2
2
6
6
12
12
20
20
20

I
Indet.
Indet.
1
1
2
2
3
3
4
4
5
5
5

Salida

Suma=20

You might also like