PHP - relacionar dos funciones php

 
Vista:
sin imagen de perfil

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:
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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

relacionar dos funciones php

Publicado por xve (6935 intervenciones) el 23/05/2016 17:47:16
Hola Aurelio, segun veo, parece que la funcion esPrimo() esta dentro de la funcion esPar(), por eso solo se ejecuta si el numero es par...

Es importante tabular correctamente... te hubieras dado cuenta!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de kip
Val: 2.325
Plata
Ha disminuido 1 puesto en PHP (en relación al último mes)
Gráfica de PHP

relacionar dos funciones php

Publicado por kip (877 intervenciones) el 23/05/2016 19:46:58
Aqui te dejo algo rapido que configure de tu codigo, espero te sirva:

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
<?php
if(isset($_POST['num']))
{
    $num = $_POST['num'];
    if ($num != ''){
         esPar($num);}
    else {
        echo "<h3 align='center'>Debe ingresar un numero </h3>";
    }
}
 
function esPar($num)
{
    if ($num % 2 == 0) {
        echo "<div style='text-align: center'><b>El numero $num es par</b></div></br>";
 
    } else {
        echo "<div style='text-align: center'><b>El numero $num es impar".primo($num)."</b></div></br>";
 
    }
}
 
function primo($num)
{
    $cont=0;
 
        for ($i=1; $i <=$num; $i++) {
            if ($num % $i==0) {
                $cont=$cont+1;
            }
        }
 
        if ($cont==2) {
            return " y primo ";
 
        }
        else {
            return " y no es primo ";
        }
 
}
 
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<form action="" 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>

Ejecuta el codigo y te daras cuenta que cumple con las condiciones que deseas, ajustalo a tus necesidades.

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar