LENGUAJES DE PROGRAMACIÓN  
 
 
FreePascal

PASCAL CON FREE PASCAL

 
 

 

4.9. Respuestas a las preguntas y ejercicios.

 

anterior :: indice :: siguiente

 

1.

 

Es el conjunto de instrucciones que se ejecutan una tras otra en el orden en que se escriben.

 

2.

 

Son las instrucciones que permiten elegir distintos caminos de ejecución en función del resultado de una instrucción o del valor de una variable.

 

3.

 

Son las instrucciones que se ejecutan repetidamente.

 

4.

 

Indique si los siguientes programas se pueden compilar:

 

No se puede compilar: A,B.

Se puede compilar: C

 

5.

 

Indique cual de los siguientes programas crea un bucle infinito:

 

Crean un bucle infinito: B,C.

No crean un bucle infinito: A,D.

     

6.

 

Cree los programas que se describen a continuación, usando sólo la estructura if-then-else:

 

   

 

 

A)

 
{$codepage UTF8}
VAR a,b:extended;
BEGIN
 Write('Ingrese numero 1 :');
 Readln(a);
 Write('Ingrese numero 2 :');
 Readln(b);
 if a=b then Writeln('estos numeros son iguales')
 else if a>b then Writeln('El mayor es : ',a:4:0)
 else Writeln('El mayor es : ',b:4:0)
END.
   

B)

 

 
{$codepage UTF8}
VAR a,b,c,menor,mayor:extended;
BEGIN
  Write('Ingrese numero 1 :');Readln(a);
  Write('Ingrese numero 2 :');Readln(b);
  Write('Ingrese numero 3 :');Readln(c);
  Writeln('La suma es  : ',a+b+c:4:0);
  Writeln('El promedio : ',(a+b+c)/3:4:0);
  Writeln('El producto : ',a*b*c:4:0);
  menor:=a;
  if b<menor then menor:=b;
  if c<menor then menor:=c;
  mayor:=a;
  if b>mayor then mayor:=b;
  if c>mayor then mayor:=c;
  Writeln('El mayor es : ', mayor:4:0);
  Writeln('El menor es : ', menor:4:0)
END.
   

C)

 

 
{$codepage UTF8}
VAR a,b,c,d,e,mayor,menor:longint;
BEGIN
  Write('Ingrese numero 1 :');Readln(a);
  Write('Ingrese numero 2 :');Readln(b);
  Write('Ingrese numero 3 :');Readln(c);
  Write('Ingrese numero 4 :');Readln(d);
  Write('Ingrese numero 5 :');Readln(e);
  mayor:=a;
  if b>mayor then mayor:=b;
  if c>mayor then mayor:=c;
  if d>mayor then mayor:=d;
  if e>mayor then mayor:=e;
  menor:=a;
  if b<menor then menor:=b;
  if c<menor then menor:=c;
  if d<menor then menor:=d;
  if e<menor then menor:=e;
  Writeln('El mayor es : ',mayor);
  Writeln('El menor es : ',menor);
END.
   

D)

 

 
{$codepage UTF8}
VAR a:longint;
BEGIN
  Write('Ingrese un numero : ');
  Readln(a);
  if ((a mod 2)=0) then Writeln('el numero es par')
                   else Writeln('el numero es impar')
END.
   

E)

 

 
{$codepage UTF8}
VAR ID:STring;
    tasa:extended;
    horas:extended;  
    pagobruto:extended;
BEGIN
  Write('Ingrese ID    : ');Readln(ID);
  Write('Ingrese tasa  : ');Readln(tasa);
  Write('Ingrese horas : ');Readln(horas);
  if horas>40 then pagobruto:=(40*tasa)+((horas-40)*1.5*tasa)
              else pagobruto:=horas*tasa;
  Writeln('Su pagobruto es : ',pagobruto:10:10)
END. 
     

F)

 

 
{$codepage UTF8}
VAR Ventas,Comision:extended; 
BEGIN
  Write('Ingrese las Ventas : ');Readln(Ventas);
  if Ventas<=50 then Comision:=0
  else if Ventas<=500 then Comision:=Ventas*0.1
  else Comision:=Ventas*0.08;
  Writeln('Su comision es : ',comision:10:10)
END. 
     

G)

 

 
{$codepage UTF8}
VAR Bisiesto:boolean;
    anio:integer;
BEGIN
    Bisiesto:=false;
Write('Ingrese el año : ');
Readln(anio);
if (((anio mod 4)=0) and ((anio mod 100)<>0)) or ((anio mod 400)=0) then Bisiesto:=true;
if Bisiesto
Then Writeln('El año ingresado es Bisiesto')
else Writeln('El año ingresado no es Bisiesto'); END.

 

   

7.

 

Cree los programas que se describen a continuación, usando sólo la estructura Case-of :

 

   

 

 

A)

 
{$codepage UTF8}
VAR Nota1,Nota2,Nota3,Nota4 : integer;
    media                   : integer;
BEGIN
  Write('Nota 1 :');Readln(Nota1);
  Write('Nota 2 :');Readln(Nota2);
  Write('Nota 3 :');Readln(Nota3);
  Write('Nota 4 :');Readln(Nota4);
  media:=(Nota1+Nota2+Nota3+Nota4) div 4;
  Case media of
    0..59  : Writeln('Puntuacion : E');
   60..69  : Writeln('Puntuacion : D');
   70..79  : Writeln('Puntuacion : C');
   80..89  : Writeln('Puntuacion : B');
   90..100 : Writeln('Puntuacion : A');
  End
END.
     

B)

 
{$codepage UTF8}
VAR salario : integer;
    nuevosalario:extended;
BEGIN
  Write('Ingrese su salario : ');Readln(salario);
  Case salario of
     0..9000 : nuevosalario:=salario*1.2;
     9001..15000 : nuevosalario:=salario*1.1;
     15001..20000 : nuevosalario:=salario*1.05;
   End;
   Writeln('Su nuevo salario : ',nuevoSalario:4:4,' unidades monetarias')
END.
     

C)

 

 
{$codepage UTF8}
VAR TVenta : Integer;
    desc   : extended;
BEGIN
  desc:=0;
  Write('Ingrese Total Venta :');
  Readln(TVenta);
  Case TVenta of
     0..100: desc:=0;
     101..2000: desc:=TVenta*0.05;
     2001..5000: desc:=TVenta*0.1;
     5001..10000: desc:=TVenta*0.15;
   End;
   Writeln('Descuento : ',desc:4:4);
   Writeln('Nuevo total Venta : ',TVenta-desc:4:4)
END. 
     

D)

 

 
{$codepage UTF8}
VAR cond1,cond2:boolean;
    Tarjeta:Char;
    importe,descuento:extended;
    op:integer;
BEGIN
  Write('Paga con tarjeta :');Readln(Tarjeta); 
  Write('Importe :');Readln(importe); 
  cond1:=(Tarjeta='S') or (Tarjeta='s');
  cond2:=importe>100;
  op:=(ord(cond1)*2)+(ord(cond2)*1);
  Case op of
    0 : descuento:=importe*0.025;
    1 : descuento:=importe*0.02;
    2 : descuento:=importe*0.05;
    3 : descuento:=importe*0.03;
  End;
  Writeln('Descuento : ',descuento:4:4);
  Writeln('Nuevo importe : ',importe-descuento:4:4)
END. 
     

E)

 

 
{$codepage UTF8}
VAR numero:integer;
BEGIN
  Write('numero : ');
  Readln(numero);
  Case numero of
    1:Writeln('Lunes');
    2:Writeln('Martes');
    3:Writeln('Miercoles');
    4:Writeln('Jueves');
    5:Writeln('Viernes');
    6:Writeln('Sabado');
    7:Writeln('Domingo');
  else Writeln('Dia no valido')
  End
END.
     

F)

 

 
{$codepage UTF8}
VAR dia,mes,anio:integer;
BEGIN
  Write('dia : ');
  Readln(dia);
  Write('mes : ');
  Readln(mes);
  Write('Dia : ');
  Case dia of
    1:Writeln('Lunes');
    2:Writeln('Martes');
    3:Writeln('Miercoles');
    4:Writeln('Jueves');
    5:Writeln('Viernes');
    6:Writeln('Sabado');
    7:Writeln('Domingo');
  else Writeln('Dia no valido,  ')
  End;
  Write('Mes : ');
  Case mes of
    1:Writeln('Enero'); 
    2:Writeln('Febrero'); 
    3:Writeln('Marzo'); 
    4:Writeln('Abril'); 
    5:Writeln('Mayo'); 
    6:Writeln('Junio'); 
    7:Writeln('Julio'); 
    8:Writeln('Agosto'); 
    9:Writeln('Septiembre'); 
    10:Writeln('Octubre'); 
    11:Writeln('Noviembre');  
    12:Writeln('Diciembre');  
  else Writeln('Dia no valido,  ')
  End
END.

 

   

8.

 

Cree los programas que se describen a continuación, usando sólo la estructura While-do :

 

   

 

 

A)

 
{$codepage UTF8}
VAR Kilometraje,Galones : Extended;
    seguir : Char;
    SKilometraje,SGalones : Extended;
BEGIN
 seguir := 's';
 SKilometraje:=0;
 SGalones:=0;
 While (seguir='s') or (seguir='S') do
  Begin
   Write('Galones          : ');Readln(Galones);
   Write('Kilometraje          : ');Readln(Kilometraje);
   Writeln('Kilometraje por galon : ',Kilometraje/Galones:10:10);
   SKilometraje:=SKilometraje+Kilometraje;
   SGalones:=SGalones+Galones;
   Write('Desea seguir : ');Readln(seguir);
  End;
 Writeln('El promedio Kilometraje/galon es : ',SKilometraje/SGalones:10:10)
END.
     

B)

 

 
{$codepage UTF8}
VAR Cuenta,Binicial,Cargo,LimCred,Bnuevo,Creditos:Extended;
    Seguir : char;
BEGIN
  seguir:='s';
  While (seguir='s') or (seguir='S') do
    Begin
      Write('Nº de Cuenta        : ');Readln(Cuenta);
      Write('Balance Inicial     : ');Readln(Binicial);
      Write('Total Articulo      : ');Readln(Cargo);      
      Write('Limite de Credito   : ');Readln(LimCred);
      Write('Creditos            : ');Readln(Creditos);
      Bnuevo:=Binicial+cargo-Creditos;
      Writeln('Balance Nuevo       : ',Bnuevo:10:4);
      if Bnuevo>LimCred then
         Begin
           Writeln('Nº de Cuenta        : ',Cuenta:10:4);
           Writeln('Limite de Credito   : ',LimCred:10:4);
           Writeln('Balance Nuevo       : ',Bnuevo:10:4);
           Writeln('Limite de credito excedido');
         End;
      Write('Desea seguir : ');Readln(seguir);
    End
 END.
     

C)

 

 
{$codepage UTF8}
VAR VntNetas,Sueldo : Extended;
    Seguir   : Char;
BEGIN
  seguir:='s';
  While (seguir='s') or (seguir='S') do
    Begin
      Write('Ventas Netas : ');Readln(VntNetas);
      Sueldo:=150+(VntNetas*0.09);
      Writeln('Sueldo : ',Sueldo:10:3);
      Write('Desea seguir : ');Readln(seguir);
     End
END.
     

D)

 

 
{$codepage UTF8}
VAR normal,extra,tarifa : extended;
    pagabruta  :extended;
    seguir : char;
BEGIN
  extra:=0;
  seguir:='s';
  While (seguir='s') or (seguir='S') do
   Begin
     Write('Horas Trabajadas : ');Readln(normal);

     Write('tarifa por hora  : ');Readln(tarifa);
     if normal>40 then
       Begin
          extra:=normal-40;
          normal:=40
       End;
     pagabruta:=(normal*tarifa)+(extra*(tarifa/2));
     Writeln('Su paga bruta es : ',pagabruta:4:3);
     Write('Desea continuar : ');Readln(seguir)
   End;
 END.
     

E)

 

 
{$codepage UTF8}
VAR numero,mayor : Extended;
    Contador,vendedor : integer;
BEGIN
  Contador:=1;
  mayor:=-5;
  vendedor:=0;
  While Contador<=10 do 
    Begin
       Write(Contador,'. Unidades Vendidas : ');Readln(numero);
       if numero>mayor
          then Begin
                 mayor:=numero;
                 vendedor:=Contador;
               End;
       Contador:=Contador+1;
    End;
  Writeln('El vendedor que vendio más es : ',vendedor);
  Writeln('Con un total de : ', mayor:4:3,' unidades vendidas');
END.
     

F)

 

 
{$codepage UTF8}
VAR pi:extended;
    n : longint;
BEGIN
   pi:=4;
   n:=1;
   While n<=5000 do
     Begin
       if (n mod 2)<>0 then pi:=pi-(4/(2*n+1))
                       else pi:=pi+(4/(2*n+1));
       n:=n+1
     End;
   Writeln(pi:4:4)
END.
     

G)

 

 
{$codepage UTF8}
VAR fact,b,i:Qword;
BEGIN
  fact:=1;
  Write('Ingrese un numero no negativo: ');readln(b); //33 //66 con Qword
  i:=1;
  While i<b do
    Begin
      fact:=fact*i;
      i+=1
    End;
  Writeln(fact:4)
END.

 

   

9.

 

Cree los programas que se describen a continuación, usando sólo la estructura Repeat-until :

 

   

 

 

A)

 
{$codepage UTF8}
VAR i:longint;
BEGIN
 i:=1;
 Repeat
   Writeln(i:4,i+1:4,i+2:4);
   i:=i+3
 Until i>298
END.
     

B)

 

 
{$codepage UTF8}
VAR i,s:extended;
BEGIN
  i:=1;
  s:=0;
  Repeat
   s:=s+(i/(i+1));
   i:=i+1;
  Until i>99;
  Writeln('suma : ',s:4:4)
END.
     

C)

 

 
{$codepage UTF8}
VAR n,i:longint;
    prod : extended;
BEGIN
  prod:=1;
  i:=1;
  Write('Ingrese un numero mayor o igual a 10 : ');
  Readln(n);
  if (n>=10) then
     Begin
       Repeat
         prod:=prod*(2*i-1)/(i*i);
         i:=i+1;
       Until i>n
     End
  else prod:=0;
  Writeln('El producto es : ',prod)
END.
     

D)

 

 
{$codepage UTF8}
VAR a,b,c:longint;
BEGIN
  a:=1;b:=1;
  Writeln('Los numeros de la serie de Fibonacci son : ');
  Writeln(a);
  Writeln(b);
  c:=a+b;
  Repeat 
    Writeln(c);
    a:=b;
    b:=c;
    c:=a+b
  Until c>10000;
END.
     

E)

 

 
{$codepage UTF8}
VAR numero,mayor,n,i:longint;
BEGIN
  Write('Numeros a leer : ');Readln(n);
  i:=1;
  mayor:=-5;
  Repeat
    Write('[',i,'] :');Readln(numero);
    if numero>mayor then mayor:=numero;
    i:=i+1
  Until i>n;
  Writeln('El numero mayor es : ',mayor)
END.
     

F)

 

 
{$codepage UTF8}
VAR n:longint;
    pobA,pobB:extended;
    TasaA,TasaB:extended;
BEGIN
  n:=0;
  Write('Poblacion A : ');Readln(pobA);
  Write('Poblacion B : ');Readln(pobB);
  Write('Tasa A : ');Readln(TasaA);
  Write('Tasa B : ');Readln(TasaB);
  Writeln('ANIO POBLACION-A POBLACION-B');
  Writeln;
  Repeat
    n:=n+1;
    pobA:=pobA*(1+TasaA);
    pobB:=pobB*(1+TasaB);
    Writeln(n:3,pobA/1000:12:0,pobB/1000:12:0);
  Until (pobA>pobB)
END.

 

   

10.

 

Cree los programas que se describen a continuación, usando sólo la estructura For-to-do :

 

   

 

 

A)

 
{$codepage UTF8}
VAR Capital,Tasa:Extended;
    i,n : longint;
BEGIN
  Write('Ingrese Capital       : ');Readln(Capital);
  Write('Ingrese Tasa          : ');Readln(Tasa);
  Write('Ingrese Periodo en años : ');Readln(n);
  Writeln;  
  Writeln('Año Capital');
  For i:=1 to n do
    Begin
      capital:=capital*(1+tasa);
      Writeln(i:2,capital:10:2)
    End
END.
     

B)

 

 
{$codepage UTF8}
USES math;
VAR Raiz:extended;
    i:longint;
BEGIN
 Writeln('Nº  raiz cuadrada  raiz cubica  raiz cuarta');
 Writeln;
 For i:= 1 to 100 do
    Writeln(i:2,i**(1/2):14:3,i**(1/3):13:3,i**(1/4):13:3);
END.
     

C)

 

 
{$codepage UTF8}
USES math;
VAR b,i:longint;
    dec:byte;
BEGIN
  dec:=0;
  Write('Ingrese un numero binario de 8 digitos : ');readln(b);
  for i:=0 to 7 do
    Begin
      dec:=dec+((b mod 10)*(2**i));
      b:=b div 10
    End;
  Writeln('Su equivalente decimal es : ',dec)
END.
     

D)

 

 
{$codepage UTF8}
VAR fact,i,e:extended;
    j : longint;
BEGIN
  e:=1;
  for j:=1 to 1000 do
   Begin
     i:=1;
     fact:=1;
     While i<=j do
       Begin
         fact:=fact*i;
         i:=i+1
       End;
     e:=e+(1/fact);
   End;
  Writeln(e:10:40)
END.
     

E)

 

 
{$codepage UTF8}
USES math;
VAR fact,i,e,x:extended;
    j : longint;
    pot:extended;
BEGIN
  e:=1;
  Write('Exponente : ');Readln(x);
  for j:=1 to 1000 do
    Begin
      i:=1;
      fact:=1;
      While i<=j do
        Begin
          fact:=fact*i;
          i:=i+1
        End;
      pot:=x**j;
      e:=e+((pot)/fact)
    End;
  Writeln(e:10:40)
END.
     

F)

 

 
{$codepage UTF8}
VAR i:longint;
BEGIN
  For i:=1 to 100 do
    if ((i mod 2)<>0) and ((i mod 7)<>0) then Writeln(i:4)
END.

 

   

11.

 

Cree los programas que se describen a continuación, usando sólo la estructura For-downto-do : :

 

   

 

 

A)

 
{$codepage UTF8}
USES math;
VAR i:longint;
    suma,pi:extended;
BEGIN
  suma:=0;
  for i:=8000 downto 1 do suma:=suma+1/i/i;
  pi:=(suma*6)**(1/2);
  Writeln(pi:12:10)
END.
     

B)

 

 
{$codepage UTF8}
VAR i,j:longint;
    suma,pi:extended;
BEGIN
  suma:=0;
  for i:=20 downto 1 do
    Begin
      For j:= 1 to i do Write(i:3);
      Writeln
    End
END.
     

D)

 

 
{$codepage UTF8}
VAR i : longint;
BEGIN
  For i:= 100 downto 1 do
    if (i mod 2)<>0 then Write(i:5)
END.
     

E)

 

 
{$codepage UTF8}
VAR n,i:longint;
    a,b,suma:extended;
BEGIN
  suma:=0;
  Write('n : ');Readln(n);
  Write('a : ');Readln(a);
  Write('b : ');Readln(b);
  for i:=n downto 0 do suma:=suma+(1/(a+i*b));
  Writeln('Suma : ',suma:4:4)
END.
     

anterior :: indice :: siguiente

 

 
 

  SUGERENCIAS