Esercizio: dispari-pari - Dipartimento di Informatica

annuncio pubblicitario
Esercizio: dispari-pari
Dato un numero, verificare se è pari o dispari e stampare il relativo
messaggio
start
start
read N
while (N > 1)
read N
N := N – 2
end while
vero
N := N - 2
N>1
if (N = 0) then
falso
vero
N=0
write "pari"
write “pari”
falso
else
write "dispari"
write “dispari”
end if
end
end
Fondamenti di Informatica -- R.Gaeta
94
Esercizio: dispari-pari (controllo input)
Dato un numero stampare se è pari o dispari
start
start
read N
N<0
vero
N := -N
falso
vero
N := N - 2
N>1
falso
vero
N=0
write "pari"
falso
write "dispari"
end
read N
if (N < 0) then
N := -N
end if
while (N > 1)
N := N – 2
end while
if (N = 0) then
write “pari”
else
write “dispari”
end if
end
Fondamenti di Informatica -- R.Gaeta
95
Esercizio: minimo di una sequenza di K numeri
• Si supponga di fornire in input ad un programma un
numero K e K interi positivi. Il programma deve restituire
il valore minimo tra quelli introdotti.
Fondamenti di Informatica -- R.Gaeta
96
Esercizio: minimo di una sequenza di K numeri
start
start
read K
K
read numero
read numero
min := numero
min := numero
inseriti := 1
while (inseriti < K)
inseriti := 1
inseriti < K
falso
read numero
write min
if (numero < min) then
vero
read numero
vero
min := numero
min := numero
end
end if
numero < min
inseriti := inseriti + 1
end while
falso
inseriti := inseriti + 1
write min
end
Fondamenti di Informatica -- R.Gaeta
97
Esercizio: minimo di una sequenza di K numeri
(controllo input)
start
start
K
K <= 0
vero
write "K deve
essere positivo!"
falso
read numero
min := numero
inseriti := 1
inseriti < K
falso
write min
vero
read numero
vero
min := numero
end
numero < min
falso
inseriti := inseriti + 1
end
read K
if ( K <= 0) then
write “K deve essere positivo!”
else
read numero
min := numero
inseriti := 1
while (inseriti < K)
read numero
if (numero < min) then
min := numero
end if
inseriti := inseriti + 1
end while
write min
end if
Fondamenti di Informatica -- R.Gaeta
98
Esercizio: elevamento a potenza
Data la base e l’esponente calcolare l’elevamento a potenza
start
start
potenza:= 1
potenza := 1
read B,E
read B,E
while (E > 0)
potenza := potenza * B
falso
write potenza
E>0
E := E - 1
vero
potenza:= potenza * B
end while
end
write potenza
end
E := E -1
Fondamenti di Informatica -- R.Gaeta
99
Esercizio: elevamento a potenza (controllo input)
Data la base e l’esponente calcolare l’elevamento a potenza
start
start
read B,E
read B,E
E >= 0
vero
if (E >= 0) then
potenza := 1
falso write"esponente
while (E > 0)
negativo!"
potenza := potenza * B
potenza := 1
E := E - 1
end while
falso
E>0
write potenza
write potenza
vero
potenza:= potenza * B
else
end
write “esponente negativo!”
end if
E := E -1
end
Fondamenti di Informatica -- R.Gaeta
100
Esercizio: fattoriale
Dato un numero calcolare il suo fattoriale
start
start
fattoriale:= 1
fattoriale := 1
read N
read N
while (N > 0)
fattoriale := fattoriale * N
falso
write fattoriale
N>0
N := N - 1
vero
fattoriale:= fattoriale * N
N:= N -1
end while
end
write fattoriale
end
Fondamenti di Informatica -- R.Gaeta
101
Esercizio: fattoriale (controllo input)
Dato un numero calcolare il suo fattoriale
start
start
read N
read N
N >= 0
vero
if (N >= 0) then
fattoriale := 1
falso write"numero
negativo!"
while (N > 0)
fattoriale := fattoriale * N
fattoriale:= 1
N := N - 1
end while
falso
N>0
write fattoriale
write fattoriale
vero
fattoriale:= fattoriale * N
else
end
write “numero negativo!”
end if
N := N -1
end
Fondamenti di Informatica -- R.Gaeta
102
Scarica