PHP Girilen Sayının Asal Olup Olmadığını Hesaplama

sdkbyrm

webmasterfrm
Üyelik Tarihi
23 Aralık 2020
Mesajlar
813
Beğeniler
1
Ticaret: 0 / 0 / 0
Bu örneğimizde kullanıcının klavyeden girmiş olduğu bir sayının asal sayı olup olmadığını bulan ve sonucu ekranda gösteren php kodlarını yapacağız. Bir tane sayı girmek için form sayfası bir tane de girilen sayının incelenmesini sağlayan php sayfası oluşturacağız. Bu sayfaların çalışan kodları aşağıda bulunmaktadır. Sayfanın altında da bir uygulama linki mevcuttur. İsterseniz kendiniz de deneyebilirsiniz.


asalsayi1.php dosyası 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

<!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 Asal Sayı Bulma-1</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>

<form action="asalsayi2.php" method="post">
<table border="0" bgcolor="#FF6666">
<tr bgcolor="#00FF99">
<td colspan="2" align="center">PHP Asal Sayı Bulma</td>
</tr>
<tr>
<td>Bir Sayı Giriniz:</td>
<td><input name="sayi" type="text" /></td>
</tr>

<tr bgcolor="#00FF99">
<td>&nbsp;</td>
<td><input name="gonder" type="submit" value="Sayıyı İncele" /></td>
</tr>
</table>

</form>
<br />

</body>
</html>


asalsayi2.php dosyası 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
55
56
57
58
59

<!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 Asal Sayı Bulma-2</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
<?php
$sayi=$_POST['sayi'];

$asal=0; $i=2;

do
{
if ($sayi % $i == 0)
{
$asal = 1;
}
$i++;
}
while($i<$sayi);

if ($asal != 1)
{
$sonuc="Sayı Asaldır";
}
else
{
$sonuc="Sayı Asal Değildir";
}

?>

<table width="435" border="1" bgcolor="#FFFF66">
<tr bgcolor="#00CCCC">
<td colspan="2" align="center">PHP Asal Sayı Bulma</td>
</tr>
<tr bgcolor="#99FF66">
<td width="206">Girilen Sayı:</td>
<td width="213"><?php echo $sayi; ?></td>
</tr>
<tr bgcolor="#CC9966">
<td colspan="2">

<h1><?php echo $sonuc; ?> </h1>


</td>
</tr>
</table>
<A HREF="javascript:javascript:history.go(-1)">Geri dön</A>
<br />

</body>
</html>
Ekran Görüntüleri:
1608738746760.png
1608738754301.png
1608738769706.png1608738776612.png
 
Üst