Darmowe Forum
Maj 07, 2024, 08:14:30 *
Witamy, Gość. Zaloguj się lub zarejestruj.
Czy dotarł do Ciebie email aktywacyjny?

Zaloguj się podając nazwę użytkownika, hasło i długość sesji
Aktualności: Forum zostało uruchomione!
 
   Strona główna   Pomoc Zaloguj się Rejestracja  
Strony: [1]
  Drukuj  
Autor Wątek: Rekurencja  (Przeczytany 3225 razy)
admin
Administrator
Ekspert
*****
Wiadomości: 821


Email
« : Luty 06, 2012, 13:00:45 »

Rekurencja - odwołanie podprogramu (procedury lub funkcji) do samego siebie.

Przykład wykorzystania rekurencji do obliczenia silni z liczby n.
program lusia;
uses crt,dos;
var x:integer;

function silnia(n:integer):integer;
begin
if n=0 then silnia:=1
else silnia:=n*silnia(n-1);
end;



begin
write(silnia(3));
readkey;
end.
« Ostatnia zmiana: Luty 13, 2012, 09:34:49 wysłane przez admin » Zapisane
admin
Administrator
Ekspert
*****
Wiadomości: 821


Email
« Odpowiedz #1 : Luty 13, 2012, 09:42:21 »

Rekurencja - silnia z wprowadzaniem wartości z klawiatury

program silni_plus;
uses crt,dos;
var x:integer;

function silnia(n:integer): integer;
begin
if n=0 then silnia:=1 else
silnia:=n*silnia(n-1);
end;

begin
clrscr;
write('Podaj liczbe= ');readln(x);
writeln('silnia z ',x,' = ',silnia(x));
readkey;
end.
Zapisane
admin
Administrator
Ekspert
*****
Wiadomości: 821


Email
« Odpowiedz #2 : Luty 13, 2012, 10:11:25 »

Rekurencja sumowanie liczb od 1 do podanej wartości ale bez liczby zero.

program silni_plus;
uses crt,dos;
var x:integer;

function suma(n:integer): integer;
begin
if n=1 then suma:=1 else
suma:=n+suma(n-1);
end;

begin
clrscr;
write('Podaj liczbe= ');readln(x);
writeln('suma z ',x,' = ',suma(x));
readkey;
end.
Zapisane
Strony: [1]
  Drukuj  
 
Skocz do:  

Powered by SMF 1.1.11 | SMF © 2006-2008, Simple Machines LLC | Sitemap

Polityka cookies
Darmowe Fora | Darmowe Forum

vocaloid-world trzywahaty pln dark-paradise yukon