La divisibilité en pascal

Si je vous demande comment on vérifie si un entier est divisible ou pas par 2 par exemple, vous aller me répondre par un simple if (nb mod 2 = 0). Certe c’est juste, mais si mon nombre comporte plus de 10 chiffres, sachant que le type de variable qui accepte la plus grande valeur est la longint, notre programme ne fonctionnera plus car on a dépacer la limite pour ce type.
Dans cet article, vous aller voir comment utiliser le type « chaine de caractère » pour effectuer la divisibilité par 2,3,4,5,6,9,10 et 25.
Sans plus attendre, je vous laisse avec les principes et leurs fonctions:
Divisibilité par 2
Principe:
Un entier est divisible par 2 si son chiffre d’unités est divisible par 2.
function div2(ch:string):boolean; var n,e:integer; begin div2:=false; ch:=ch[length(ch)]; val(ch,n,e); if (n mod 2=0) then div2:=true; end;
fonction divisibilité par 2
Divisibilité par 3
Principe:
Un entier est divisible par 3 si la somme de ses chiffres est divisible par 3.
function div3 (ch:string):boolean; var i,n,e,s:integer; begin div3:=false; s:=0; n:=0; for i:=1 to length(ch) do begin val (ch[i],n,e); s:=s+n; end; if (s mod 3=0) then div3:=true; end;
fonction divisibilité par 3
Divisibilité par 4
Principe:
Un entier est divisible par 4 si ses deux derniers chiffres sont divisibles par 4.
function div4 (ch:string):boolean; var n,e:integer; begin div4:=false; ch:=ch[length(ch)-1]+ch[length(ch)]; val(ch,n,e); if (n mod 4=0) then div4:=true; end;
fonction divisibilité par 4
Divisibilité par 5
Principe:
Un entier est divisible par 5 si son chiffre d’unité est 0 ou 5.
function div5 (ch:string):boolean; begin div5:=false; if (ch[length(ch)] in ['0','5']) then div5:=true; end;
Fonction divisibilité par 4
Divisibilité par 6
Principe:
Un entier est divisible par 6 s’il est divisible par 2 et 3.
On va utiliser ici les deux fonction précédentes (div2 et div3).
if (div2(nch)= true) and (div3_9(nch,3)=true) then
div6:=true;
Instruction pour la divisibilité par 6
Divisibilité par 9
Principe:
Le même que celui du 3, il faut que la somme de ses chiffres est divisible par 9.
function div9 (ch:string):boolean; var i,n,e,s:integer; begin div9:=false; s:=0; n:=0; for i:=1 to length(ch) do begin val (ch[i],n,e); s:=s+n; end; if (s mod 9=0) then div9:=true; end;
Fonction divisibilité par 9
Divisibilité par 10
Principe:
Un entier est divisible par 10 si le chiffre d’unité égale 0.
function div10(ch:string):boolean; begin div10:=false; if (ch[length(ch)]='0') then div10:=true; end;
Fonction divisibilité par 10
Divisibilité par 25
Principe:
Même que celui du 4, un entier est divisible par 25 si ses deux derniers chiffres sont divisible par 25.
function div25 (ch:string):boolean; var n,e:integer; begin div25:=false; ch:=ch[length(ch)-1]+ch[length(ch)]; val(ch,n,e); if (n mod 25=0) then div25:=true; end;
Fonction divisibilité par 25
Programme pascal
program divisibilite;
uses wincrt;
var nch:string;
d,encore:integer;
res:boolean;
function div2(ch:string):boolean;
var n,e:integer;
begin
div2:=false;
ch:=ch[length(ch)];
val(ch,n,e);
if (n mod 2=0) then
div2:=true;
end;
function div3_9 (ch:string;d:integer):boolean;
var i,n,e,s:integer;
begin
div3_9:=false;
s:=0;
n:=0;
for i:=1 to length(ch) do
begin
val (ch[i],n,e);
s:=s+n;
end;
if (s mod d=0) then
div3_9:=true;
end;
function div4_25 (ch:string;d:integer):boolean;
var n,e:integer;
begin
div4_25:=false;
ch:=ch[length(ch)-1]+ch[length(ch)];
val(ch,n,e);
if (n mod d=0) then
div4_25:=true;
end;
function div5 (ch:string):boolean;
begin
div5:=false;
if (ch[length(ch)] in ['0','5']) then div5:=true;
end;
function div10(ch:string):boolean;
begin
div10:=false;
if (ch[length(ch)]='0') then div10:=true;
end;
begin
repeat
writeln;
writeln(' #### #### #### #### #### #### ####');
write(' Donner moi votre nombre : ');
readln(nch);
write(' Par quoi voulez vous le diviser (2,3,4,5,6,9,10,25)? = ');
readln(d);
res:=false;
case d of
2: res:= div2(nch);
3: res:= div3_9(nch,d);
4: res:= div4_25(nch,4);
5: res:= div5(nch);
6: if (div2(nch)= true) and (div3_9(nch,3)=true) then
res:=true;
9: res:= div3_9(nch,9);
10:res:= div10(nch) ;
25:res:= div4_25(nch,25);
end;
writeln;
if (res=true) then writeln (' ==> Divisible!')
else writeln (' ==> non divisible!');
writeln;
write(' >>>> encore? (0,1) = ');
readln(encore);
writeln(' #### #### #### #### #### #### #### ');
writeln;
until (encore=0) ;
end.
Informez-moi par commentaire ou par Email si vous trouvez une erreur, merci.
Abonnez-vous au flux RSS et vous aurez tous les nouveaux articles délivrés pour vous!