Vous êtes sur la page 1sur 5

PROBLEMAS RESUELTOS

1. Disear un algoritmo que multiplique 2 nmeros naturales utilizando solamente sumas sucesivas procedure TForm2.Button1Click(Sender: TObject); var X,Y:shortint; //Variables a multiplicar C:shortint; //Contador P:smallint; //Acumulador begin X:=strtoint(edit1.text); //Lectura de X y Y Y:=strtoint(edit2.text); P:=0; //Inicializacin de P y C C:=0; while (C<abs(Y)) do //Se usa While para cubrir el caso Y=0 de una vez begin P:=P+X; //Sumas sucesivas = Producto C:=C+1; //Controla las repeticiones end; if Y<0 then //Condicin para los negativos P:=P-P-P; showmessage('El Producto es: '+inttostr(P)); //Escritura del Resultado end; 2. Disear un algoritmo que divida 2 nmeros naturales utilizando solamente sumas restas sucesivas procedure TForm2.Button1Click(Sender: TObject); var X,Y:shortint; //Variables a dividir C,R:shortint; //Cociente y Resto begin X:=strtoint(edit1.text); //Lectura de X y Y Y:=strtoint(edit2.text); if Y<>0 then //Ntese que si Y=0 no se ejecuta accin alguna begin R:=abs(X); //Inicializacin de Variables C:=0; while (R>=abs(Y)) do //Se usa While para contemplar el caso si X<Y begin R:=R-abs(Y); C:=C+1; end; if (X<0) then //Condicin para el Resto si X<0 R:=R-R-R; if (X<0) xor (Y<0) then //Condicin para el Cociente si X Y <0 C:=C-C-C; showmessage('El Cociente es: '+inttostr(C)+', y sobran: '+inttostr(R)); //Escritura del Resultado end; end;

3. Determinar si un nmero es par o impar utilizando solamente las operaciones de suma y resta procedure TForm2.Button1Click(Sender: TObject); const //AREA DE DECLARACIN DE CONSTANTES Y=2; //OBSERVE LA SINTAXIS EN LA DECLARACIN var X:shortint; //Variable a analizar R:shortint; //Resto Resp:String; //Respuesta begin X:=strtoint(edit1.text); //Lectura de X if X=0 then //Evaluacin del caso especial X=0 begin showmessage('Introduzca un nmero vlido'); //Mensaje de Error edit1.Text:=''; //SE BORRA EL VALOR ANTERIOR edit1.SetFocus; //SE COLOCA EL CURSOR EN EDIT1 end else begin R:=abs(X); //Inicializacin de Variables Resp:='Par'; //Asumimos que X es Par while (R>=abs(Y)) do //Se usa While para contemplar el caso si X<Y R:=R-abs(Y); if (R>0) then //Evaluacin del Resto para ver si cambia a Impar o no Resp:='Impar'; showmessage('El Nmero Introducido es: '+Resp); //Escritura del Resultado end; end; 4. Determinar el mnimo comn mltiplo y el mximo comn divisor de dos nmeros naturales M y N procedure TForm2.Button1Click(Sender: TObject); var M,N:byte; //Valores introducidos por el usuario C:byte; //Contador mcm:word; //mcm Notese el Tipo de Variable begin M:=strtoint(edit1.text); //Lectura de Variables N:=strtoint(edit2.text); if ((M<>0) and (N<>0)) then //Condicin para Evitar la divisin por 0 begin C:=1; //Inicializacin de variables repeat //Se usa Repeat porque se debe ejecutar el ciclo al menos 1 vez mcm:=M*C; inc(C); //ESTA INSTRUCCIN ES EQUIVALENTE A C:=C+1; until ((mcm mod N)=0); label1.caption:='El mcm entre '+inttostr(M)+' y '+inttostr(N)+' es: ' //OBSERVE LA CONCATENACIN +inttostr(mcm)+#13+'y su respectivo MCD es: '+inttostr((M*N) div mcm); end; end;

5. Determinar cuntos das hay entre dos fechas de un mismo ao procedure TForm2.Button1Click(Sender: TObject); var year:word; //Variable para el ao dia1,dia2,mes1,mes2:byte; //Variable para las fechas d1,d2,m1,m2,auxm,auxd:byte; //Variables auxiliares mes,feb:byte; //Contadores para calcular dif:word; //Respuesta Final bisiesto:boolean; //Variable de Control begin year:=strtoint(edit1.text); //Lectura de Datos dia1:=strtoint(edit2.Text);mes1:=strtoint(edit3.Text); dia2:=strtoint(edit4.Text);mes2:=strtoint(edit5.Text); bisiesto:=false; //Se asume que el ao no es bisiesto feb:=28; if (year>0) and (mes1>0) and (mes1<=12) and (mes2>0) and (mes2<=12) and (dia1>0) and (dia2>0) then //Validacin Bsica begin if ((year mod 4)=0) and (((year mod 100)<>0) or ((year mod 400)=0)) then begin //Determinacin de si el ao es bisiesto bisiesto:=true; feb:=29; end; if (((mes1 in [1,3,5,7,8,10,12]) and (dia1<=31)) or //Validacin para los das ((mes1 in [4,6,9,11]) and (dia1<=30)) or ((mes1=2) and bisiesto and (dia1<=29)) or ((mes1=2) and not(bisiesto) and (dia1<=28))) AND //OJO CON LOS PARNTESIS (((mes2 in [1,3,5,7,8,10,12]) and (dia2<=31)) or ((mes2 in [4,6,9,11]) and (dia2<=30)) or ((mes2=2) and bisiesto and (dia2<=29)) or ((mes2=2) and not(bisiesto) and (dia2<=28))) then begin d1:=dia1;m1:=mes1; //Inicializacin de variables auxiliares d2:=dia2;m2:=mes2; //Se asume que Fecha1>Fecha2 if mes2>mes1 then //Si Fecha1<Fecha2 se intercambian con este If begin auxd:=d1; d1:=d2; d2:=auxd; //FORMA CORRECTA DE INTERCAMBIAR VARIABLES auxm:=m1; m1:=m2; m2:=auxm; end else if (mes2=mes1) and (dia2>dia1) then begin auxd:=d1; d1:=d2; d2:=auxd; end; if m1=m2 then //Diferencia de Das si los meses son iguales dif:=d1-d2 else begin dif:=d1; //Das de la fecha mayor mes:=m1-1; //Contador de meses completos while (mes>m2) do //Se usa While para cubrir el caso cuando la diferencia de mes=1 begin

if (mes in [1,3,5,7,8,10,12]) then //Para meses con 31 das dif:=dif+31 else if (mes in [4,6,9,11]) then //Para meses con 30 das dif:=dif+30 else dif:=dif+feb; //Para Febrero dec(mes); //INSTRUCCIN EQUIVALENTE A mes:=mes-1; end; if (m2 in [1,3,5,7,8,10,12]) then //Suma los das de la fecha menor dif:=dif+31-d2 else if (m2 in [4,6,9,11]) then dif:=dif+30-d2 else dif:=dif+feb-d2; end; showmessage('La diferencia en das es: '+inttostr(dif)); //Escribe Resultado end; end; end; 6. Hacer un programa que determine si un nmero dado es no primo procedure TForm2.Button1Click(Sender: TObject); var X,N:word; //Nmero a analizar y Variable que verifica los divisores resp:string; //Respuesta Final begin X:=strtoint(edit1.text); //Lectura de datos if (X<2) then //Validacin que incluye mensaje showmessage('Nmero no vlido') else begin resp:=' es Primo'; //Se asume que el nmero es primo N:=2; //Se inicializan los divisores excluyendo al 1 while N<=(X div 2) do //Se buscan divisores hasta la mitad if (X mod N)=0 then begin //Si se consigue al menos uno ya no es primo y se rompe el ciclo resp:=' NO es primo'; break; end else inc(N); //NTESE QUE ESTE WHILE SLO TIENE UNA INSTRUCCIN POR LO QUE NO TIENE BEGIN END showmessage(inttostr(X)+resp); //Escribe Resultado end; end; 7. Escriba un programa para calcular la longitud de la circunferencia y el rea del crculo para un radio introducido por teclado procedure TForm2.Button1Click(Sender: TObject); var R:real; //Valor del Radio begin R:=strtofloat(edit1.text);//Lectura de R if (R>0) then showmessage('Longitud= '+floattostr(2*pi*R)+#13 +'Superficie= '+floattostr(pi*R*R));

//Una sola instruccin para calcular todo

end; 8. Hacer un programa que ordene 3 nmeros dados de mayor a menor procedure TForm2.Button1Click(Sender: TObject); var X,Y,Z:real; //Variables a ordenar aux:real; //Variable Auxiliar para intercambio begin X:=strtofloat(edit1.Text); //Lectura de datos Y:=strtofloat(edit2.Text); Z:=strtofloat(edit3.Text); //Se asume inicialmente que X>Y>Z if (Y>X) then //Si Y>X se intercambian begin aux:=X; X:=Y; Y:=aux; end; if (Z>X) then //Si ahora Z>X se intercambian quedando el mayor en X begin aux:=Z; Z:=X; X:=aux; end; if (Z>Y) then //Se ordenan los 2 restantes begin aux:=Y; Y:=Z; Z:=aux; end; showmessage('Los nmeros ordenados son:'+#13 //Escribe resultado #13 equivale a un ENTER +floattostr(X)+#13+floattostr(Y)+#13+floattostr(Z)); end; 9. Calcular la serie 1/1+1/2+1/3+..+1/N, donde N es dato procedure TForm2.Button1Click(Sender: TObject); var N,C:byte; //Variable suministrada por el usuario y contador Serie:real; //Acumulador que guarda el valor de la serie begin N:=strtoint(edit1.text); //Lectura de N if N<>0 then begin C:=1; //Inicializacin de Variables Serie:=1; while (C<N) do begin //Se usa While para cubrir el caso N=1 inc(C); Serie:=Serie+1/C; end; showmessage('El valor de la serie es: '+floattostr(Serie)); //Escribe resultado end; end;

Vous aimerez peut-être aussi