Bu örneğimizde php kodları ile Fibonacci dizisinin elemanlarını yazdıran programı yapacağız. Fibonacci dizisi her elemanı kendisinden önce gelen iki elemanının toplamı olan bir dizidir. Bu örneğimizde kullanıcıdan klavyeden fibonacci dizisinin eleman sayısını girmesini istiyoruz. Kullanıcının girmiş olduğu eleman sayısı kadar fibonacci dizisi elemanlarını bir sonraki sayfada ekrana yan yana yazdırıyoruz. Programın çalışan ekran görüntüleri ve kodları aşağıdadır. Ayrıca isteyen ziyaretçiler Kendin DENE butonuna basarak örneği kendileri de deneyebilirler.
fibonaccidizisi1.php sayfası kodları:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Fibonacci Dizisi-1</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="fibonaccidizisi2.php" method="post"> <table border="0" bgcolor="#FFFF00""> <tr> <td colspan="2" align="center">Fibonacci Dizisi Yazdırma</td> </tr> <tr> <td>Dizi Eleman Sayısı:</td> <td><input name="fibonacci" type="text" /></td> </tr> <tr> <td> </td> <td><input name="gonder" type="submit" value="Dizi Elemanlarını Yazdır" /></td> </tr> </table> </form> </body> </html> |
fibonaccidizisi2.php sayfası kodları:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Fibonacci Dizisi-2</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <?php $adet=$_POST['fibonacci']; $a="1"; $b="2"; ?> <table border="0" bgcolor="#99FF99"> <tr> <td colspan="2" align="center">Fibonacci Dizisi</td> </tr> <tr> <td>Eleman Sayısı:</td> <td><strong><?php echo $adet; ?></strong></td> </tr> <tr> <td>Dizi Elemanları:</td> <td> <?php echo "1 2 "; for ($i = 3; $i <= $adet; $i++) { $c = $a + $b; echo $c." "; $a = $b; $b = $c; } ?> </td> </tr> </table> <p><A HREF="javascript:javascript:history.go(-1)">Geri dön</A></p> </body> </html> |
Yorum Yap