Prime number in php w3schools

PHP program to check prime number:

The below program checks if a number is a prime or a composite number. The PHP echo statement is used to output the result on the screen.
Example

DOCTYPE html>
<html>
<body>
 
php
$num = 13;  
$count=0;  
for ( $i=1; $i<=$num; $i++)  
{  
if (($num%$i)==0)  
{  
$count++;  
}  
}  
if ($count<3)  
{  
echo "$num is a prime number.";  
}
else
{
echo "$num is not a prime number."; 
}
?>
 
body>
html>

Output

13 is a prime number.

Prime number in php w3schools

Please Share

Last update on August 19 2022 21:50:29 (UTC/GMT +8 hours)

PHP function: Exercise-2 with Solution

Write a function to check whether a number is prime or not.

Note: A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Pictorial Presentation:

Prime number in php w3schools

Sample Solution:

PHP Code:



Sample Output:

This is a Prime Number..

Flowchart :

Prime number in php w3schools

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument.
Next: Write a function to reverse a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

PHP: Tips of the Day

PHP: How to find the last day of the month from date?

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

Ref : https://bit.ly/31coSLD


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation


A number which is only divisible by 1 and itself is called prime number. Numbers 2, 3, 5, 7, 11, 13, 17, etc. are prime numbers.

  • 2 is the only even prime number.
  • It is a natural number greater than 1 and so 0 and 1 are not prime numbers.

Prime number in PHP

Example:

Here is the Program to list the first 15 prime numbers.

Output:

Prime number in php w3schools

Prime Number using Form in PHP

Example:

We'll show a form, which will check whether a number is prime or not.

Output:

On entering number 12, we get the following output. It states that 12 is not a prime number.

Prime number in php w3schools

On entering number 97, we get the following output. It states that 97 is a prime number.

Prime number in php w3schools


Prime number in php w3schools
For Videos Join Our Youtube Channel: Join Now


Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

Prime number in php w3schools
Prime number in php w3schools
Prime number in php w3schools





What is prime number in php?

A number which is only divisible by 1 and itself is called prime number. Numbers 2, 3, 5, 7, 11, 13, 17, etc. are prime numbers.

How do you check a number is prime or not in php?

php function check_prime($num) { if ($num == 1) return 0; for ($i = 2; $i <= $num/2; $i++) { if ($num % $i == 0) return 0; } return 1; } $num = 47; $flag_val = check_prime($num); if ($flag_val == 1) echo "It is a prime number"; else echo "It is a non-prime number" ?>

How do you check if a number is prime?

How do you know a prime number? If a number has only two factors 1 and itself, then the number is prime. Hence, by prime factorisation of the given number, we can easily determine a prime number.

What is php and example?

PHP is a server-side scripting language created in 1995 by Rasmus Lerdorf. PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.