
relacionar dos funciones php
Publicado por Aurelio (4 intervenciones) el 23/05/2016 14:48:51
En mi formluario, se debe de mostrar si el número introducido es primo o no solamente cuando el número sea impar. He consegudi hacer las funciones para que me digan si el número es par o no y si es primo o no, pero como consigo que me diga si es primo o no solamente cuando el numero introducido sea impar?
esto es lo que he hecho hasta ahora:
html:
php:
esto es lo que he hecho hasta ahora:
html:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="dcterms.created" content="do., 22 may. 2016 13:05:54 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
</head>
<body>
<form action="calcula.php" method="post">
<table border=1 bgcolor="#3B742E" align='center'>
<tr> <td><font color='white'><b>Ingrese el numero </b></font></td> <td><input type="text" name="num"> </tr>
<tr align='center'> <td colspan="2"><input type="submit" name="ejecutar" value="ejecutar"> </td> </tr>
<tr align='center'> <td colspan="2"><input type="reset" name="limpiar" value="limpiar"> </td> </tr>
</table>
</form>
</body>
</html>
php:
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
60
61
62
63
64
65
<?php
$num=0;
$num=$_POST["num"];
$cont=0;
for ($i=1; $i <=$num; $i++) {
if ($num % $i==0) {
$cont=$cont+1;
}
}
if ($cont==2) {
echo "el número es primo";
echo "<h3 align='center>";
}
else {
echo "el número no es primo";
echo"<h3 align='center>";
}
?>
<br/>
<?php
if(isset($_POST['num']))
{
$num = $_POST['num'];
}
if($num != null)
{
if(!esPar($num))
{
}
}
else
{
echo "<br>";
echo "<h3 align='center'>Debe ingresar un numero </h3>";
echo "<h3 align='center'><a href='calcularr.html'>Volver al formulario </a></h3>";
}
function esPar($num)
{
echo "<h3 align='center'><font color='#C0B840'>";
if ($num % 2 == 0)
{
echo "El numero $num es par";
echo "<a href='calcularr.html'>Volver al formulario";
return true;
}
else
{
echo "El numero $num es impar";
echo "<a href='calcularr.html'>Volver al formulario";
return false;
}
if (esPrimo($num)) {
echo "...";
} else {
echo "...";
}
}
?>
Valora esta pregunta


0