Algoritmos if y case

22
COLEGIO DE CIENCIAS Y HUMANIDADES PLANTEL ORIENTE Algoritmos de Programación If & Case Domínguez Patricio Sabina Abigail 07/03/2014

Transcript of Algoritmos if y case

Page 1: Algoritmos if y case

COLEGIO DE CIENCIAS Y HUMANIDADES PLANTEL ORIENTE

Algoritmos de Programación

If & Case

Domínguez Patricio Sabina Abigail

07/03/2014

Page 2: Algoritmos if y case

1

Programas

con IF NOTA: Todos los diagramas de

flujo están hechos en el programa

DIA.

Sobre los programas están

exportados del Block de Notas

Page 3: Algoritmos if y case

2

1.-Escribe algoritmo y programa que pida al usuario una contraseña e imprima en pantalla si es correcta.

PROGRAMA: DIAGRAMA DE FLUJO:

Program Password;

Uses Crt;

Var nip:integer;

Begin

Clrscr;

Writeln (‘Ingresa el NIP’);

Readln (nip);

if nip=1234 then

Writeln (‘El NIP es correcto’)

else

Writeln (‘El NIP es incorrecto, vuelva a intentarlo’);

Readln;

End.

Page 4: Algoritmos if y case

3

2.- Algoritmo y programa que lea 2 calificaciones, calcule el promedio e imprima en pantalla si aprobó o no aprobó.

PROGRAMA: DIAGRAMA DE FLUJO

Program Promedio;

Uses Crt;

Var C1,C2:integer;

p:real;

Begin

Clrscr;

Writeln('Ingrese la primer calificacion');

Readln(C1);

Writeln('Ingrese la segunda calificaci¢n');

Readln(C2);

Writeln('Numero de asistencias');

Readln(a);

p:=(C1+C2/2);

if (p>=6) then

Writeln('Estas aprobado con promedio de', p:0:2)

else

Writeln('­Te veo en PAE! ­Muajajajajaja!');

readln;

End.

PRUEBA DE ESCRITORIO

C1 C2 PROM R=

7 + 9 = 16/2 = 8 / Estas

aprobado con promedio de 8

Page 5: Algoritmos if y case

4

3.- Escribe algoritmo y programa que indique si un número es positivo o negativo.

PROGRAMA: DIAGRAMA DE FLUJO:

program PosNeg;

var numero: integer;

begin

writeln('Escriba un número'); readln(numero);

if numero>0 then

writeln('El número es positivo')

else

Writeln(‘El número es negativo’);

end.

PRUEBA DE ESCRITORIO:

N N>0 R=

20 T Es positivo

Page 6: Algoritmos if y case

5

4.- Algoritmo y programa que lea la edad del usuario y declare si es o no es mayor de edad.

PROGRAMA: DIAGRAMA DE FLUJO:

program adultoomenor;

uses crt;

var edad, x:integer;

begin

clrscr;

writeln('¿que edad tienes?');

readln(edad);

if edad>=18 then

writeln('­Eres mayor de edad!')

else

x:=18-edad;

writeln('eres menor de edad, te faltan ' ,

x , ' años para ser mayor de edad');

readln;

end.

Page 7: Algoritmos if y case

6

5.- Algoritmo y programa que lea 2 números y declare cuál es mayor.

PROGRAMA: DIAGRAMA DE FLUJO:

Program MayorMenor;

Uses Crt;

Var x,y:integer;

Begin

Clrscr;

Writeln(‘Ingrese el primer numero’);

Readln(x);

Writeln(‘Ingrese el segundo numero’);

Readln(y);

If x>y then

Writeln(‘El mayor es ‘, x)

else

Writeln(‘El mayor es’, y);

Readln;

End.

PRUEBA DE ESCRITORIO

X Y COMP R=

10 20 10<20 El mayor es X

Page 8: Algoritmos if y case

7

6.- Algoritmo y Programa donde se lean 3 números y se declare cuál es mayor (usando conectores).

PROGRAMA: DIAGRAMA DE FLUJO:

Program Comparacion;

Uses Crt;

Var x,y,z:integer;

Begin

Clrscr;

Writeln(‘Ingrese el primer numero’);

Readln(x);

Writeln(‘Ingrese el segundo numero’);

Readln(y);

Writeln(Ingrese el tercer numero’);

Readln(z);

if (x<>y) and (x<>z) and (y<>z) then

Begin

Clrscr;

if (x>y) and (x>z) then writeln(‘El mayor es’, x);

If (y>x) and (y>z) then Writeln(‘El mayor es’, y);

If (z>x) and (z>y) then Writeln(‘El mayor es’, z);

End

else

Writeln(‘Hay numerous iguales’);

Readln;

End;

End.

End.

PRUEBA DE ESCRITORIO:

X Y Z COMP R=

100 201 78 100<201>78 *El mayor es Y

Page 9: Algoritmos if y case

8

7.- Algoritmo y programa donde lea números y declare si es par o inpar.

PROGRAMA: DIAGRAMA DE FLUJO:

PROGRAM PAROIMPAR; USES CRT; VAR num:INTEGER; BEGIN ClrScr; WRITE (Introduzca un numero entero: '); READLN (num); IF num = 0 THEN WRITE ('El numero introducido no es par ni impar, es 0') ELSE IF ((num mod 2 = 0)) THEN WRITE ('El numero introducido es par') ELSE WRITE ('El numero introducido es impar') END.

Page 10: Algoritmos if y case

9

9.- Algoritmo y programa que calcule

PROGRAMA: DIAGRAMA DE FLUJO:

Program EcuaciónSegundoGrado;

Uses Crt;

var a,c,b,x,x1,x2,rr:real;

Begin

Clrscr;

Writeln (‘Lectura de datos’);

readln (a);

if a=-0 then

Begin

RR-B**2-4ac

if RR>0 then

X1:= (-B+sqrt*rr)/2a

x2:= (-B-sqrt*rr)/2A

Writeln (‘Ecribir resultado’)

else

Writeln (‘Soluciones complejas’);

Readln;

End

Else

x:= (-c/b);

Writeln (‘Escribir resultado’);

Readln;

End;

End.

Page 11: Algoritmos if y case

10

10.- Algoritmo y programa que lea un número e indique si tiene 1, 2, 3 o 4 o más dígitos.

Program Digitos;

Uses Crt;

Var n:integer;

Begin

Clrscr;

Writeln('Escribe un numero entero posotivo: ' );

Readln(n);

if n<=9 then

Writeln('Tiene 1 digito')

else

if n<=99 then

Writeln('Tiene 2 digitos')

else

if n<=999 then

Writeln('Tiene 3 digitos')

else

if n>=1000 then

Writeln('Tiene 4 digitos o mas...');

readln;

readln;

End.

Page 12: Algoritmos if y case

11

11.- Programa sin diagrama de un algoritmo que indique su signo zodiacal de acuerdo a la fecha de nacimiento del usuario.

program SignosZodiaco;

uses crt;

var dia, mes : integer;

var tecla : char;

begin

clrscr

write ('Ingrese el valor de dia: '); readln (dia);

write ('Ingrese el valor de mes: '); readln (mes);

if ((dia>=21) and (mes=3)) or ((dia<=20) and (mes=4)) then

begin

writeln ('Aries’); end;

if ((dia>=24) and (mes=9)) or ((dia<=23) and (mes=10)) then

begin

writeln ('Libra'); end;

if ((dia>=21) and (mes=4)) or ((dia<=21) and (mes=5)) then

begin

writeln ('Tauro'); end;

if ((dia>=24) and (mes=10)) or ((dia<=22) and (mes=11)) then

begin

writeln ('Escorpio');end;

if ((dia>=22) and (mes=5)) or ((dia<=21) and (mes=6)) then

begin

writeln ('Geminis'); end;

if ((dia>=23) and (mes=11)) or ((dia<=21) and (mes=12)) then

begin

writeln ('Sagitario'); end;

if ((dia>=21) and (mes=6)) or ((dia<=23) and (mes=7)) then

begin

Page 13: Algoritmos if y case

12

writeln ('Cancer');end;

if ((dia>=22) and (mes=12)) or ((dia<=20) and (mes=1)) then begin

writeln ('Capricornio'); end;

if ((dia>=24) and (mes=7)) or ((dia<=23) and (mes=8)) then

begin

writeln ('Leo');end;

if ((dia>=21) and (mes=1)) or ((dia<=19) and (mes=2)) then

begin

writeln ('Acuario');end;

if ((dia>=24) and (mes=8)) or ((dia<=23) and (mes=9)) then

begin

writeln ('Virgo'); end;

if ((dia>=20) and (mes=2)) or ((dia<=20) and (mes=3)) then

begin

writeln ('Piscis'); end;

writeln;

write ('Presione una tecla para terminar . . . ');

tecla := readkey;

end.

Page 14: Algoritmos if y case

13

Ejercicios

con Case

Page 15: Algoritmos if y case

14

1.- Algoritmo y programa que te lea una letra e imprima en pantalla, si es A: Aguila, si es B: Becerro, y si es C: Camello, caballo.

Program Zoologico;

Uses Crt;

Var letra:char;

Begin

Clrscr;

Writeln (‘Dame una letra’);

Readln (letra);

Case letra of

‘a’: Begin

Writeln (‘Aguila’);

Readln;

End;

‘b’: Begin

Writeln (‘Becerro’);

Readln;

End;

‘c’: Begin

Writeln (‘Camello’);

Readln;

End

else

Begin : Writeln (‘No hay’); readln; End; End; End.

Page 16: Algoritmos if y case

15

2.- Algoritmo y programa que te lea una letra e imprima en pantalla, si es A: Aguila, si es B: Becerro, y si es C: Camello, caballo (Internet):

program prog3;

uses crt;

var

letra:char;

begin

clrscr;

gotoxy(20,5);write('dame una letra :');

readln(letra);

(* empieza case *)

case letra of

'a': begin gotoxy(50,5); write('aguila'); readln; end;

'b','B': begin gotoxy(50,5); write('becerro'); readln; end;

'c': begin gotoxy(50,5); write('caballo ');write('camello'); readln; end;

else begin gotoxy(50,5);write('no hay');readln; end

end;

end.

Page 17: Algoritmos if y case

16

3.- Algoritmo y programa que lea un número e indique si tiene 1, 2, 3 o 4 o más dígitos.

Program Digitos;

Uses Crt;

Var n:integer;

Begin

Clrscr;

Writeln('Escribe un numero entero posotivo: ' );

Readln(n);

Case n of

1..9:

Begin

Writeln('El numero tiene 1 digito');

Readln;

End;

10..99:

Begin

Writeln('El numero tiene 2 digitos');

Readln;

End;

100..999:

Begin

Writeln('El numero tiene 3 digitos');

Readln;

End;

1000..9999:

Begin

Writeln('El numero tiene 4 digitos o m s');

Readln;

End;

End;

End.

Page 18: Algoritmos if y case

17

4.- Programa q muestre las 4 operaciones aritméticas (sumar restar multiplicar dividir) (del menú anterior que quiere hacer?)

Program Operaciones;

Uses Crt;

Var a,b:real

var numero:integer

var suma,rest,mult:REAL;

VAR divi:REAL;

Begin

Clrscr;

Writeln (‘¿Qué operación aritmética deseas ejecutar?’);

Writeln (‘Suma= 1, resta=2, multiplicación=3, división=4’);

Readln (numero);

Case numero of

‘1’: Begin If numero=1 then

Writeln (‘Escribe el primer sumando’);

Readln (a);

Writeln (‘Escribe el segundo sumando’);

Readln (b);

suma:= (a+b);

Writeln (‘La suma es de: ‘ , suma);

Readln;

End;

‘2’: Begin If numero=2 then

Writeln (‘Escribe el valor del minuendo’);

Readln (a);

Writeln (‘Escribe el valor del sustraendo’);

Readln (b);

rest:= (a-b)

Page 19: Algoritmos if y case

18

Writeln (‘La resta es de: ‘, rest);

Readln;

End;

‘3’: Begin If numero=3 then

Writeln (‘Escribe el primer valor a multiplicar’);

Readln (a);

Writeln (‘Escribe el segundo valor a multiplicar’);

Readln (b);

mult: (a*b);

Writeln (‘La multiplicación es de: ‘, mult);

Readln;

End;

‘4’: Begin If numero=4 then

Writeln (‘Escribe el valor del divisor’);

Readln (a);

Writeln (‘Escribe el valor del dividendo’);

Readln (b);

divi:= (a/b);

Writeln (‘La division es de: ‘, divi:0:2);

Readln;

End;

End.

Page 20: Algoritmos if y case

19

6.- Programa que calcule el área de rectángulo, circulo triangulo, cuadrado.

Program Areas;

Uses Crt;

Var base,altura,lado,radio:real

var numero:integer

var rec,trian,cir,cuadr:REAL;

Begin

Clrscr;

Writeln (‘¿Qué área deseas calcular?’);

Writeln (‘Rectágulo=1,Círculo=2,triangulo=3,cuadrado=4’);

Readln (numero);

Case numero of

‘1’: Begin If numero=1 then

Writeln (‘Escribe el valor de la base’);

Readln (base);

Writeln (‘Escribe el valor de la altura’);

Readln (altura);

rec:=(base*altura);

Writeln (‘El area es de: ‘ , rec);

Readln;

End;

‘2’: Begin If numero=2 then

Writeln (‘Escribe el valor del radio’);

Readln (radio);

cir:=(3.141592*radio*radio);

Writeln (‘La resta es de: ‘, rest);

Readln;

End;

Page 21: Algoritmos if y case

20

‘3’: Begin If numero=3 then

Writeln (‘Escribe el valor de la base’);

Readln (base);

Writeln (‘Escribe el valor de la altura’);

Readln (altura);

trian:= (base*altura)/2

Writeln (‘El área del triangulo es de: ‘, trian:0:2);

Readln;

End;

‘4’: Begin If numero=4 then

Writeln (‘Escribe el valor del lado’);

Readln (lado);

cuadr:=(lado*lado);

Writeln (‘El área del cuadrado es de: ‘, cuadr);

Readln;

End;

End.

Page 22: Algoritmos if y case

21

7.- Escribe un programa que elabore una serie numérica.

Program Serie;

Uses Crt;

Var n:integer;

Begin

Clrscr;

n:=0

while n<=10 do

Writeln (n);

n:=(n+1);

repeat;