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>
Output
13 is a prime number.
Please Share
Write a function to check whether a number is prime or not.PHP function: Exercise-2 with Solution
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:
Sample Solution:
PHP Code:
Sample Output:
This is a Prime Number..
Flowchart :
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 : //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 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.
On entering number 97, we get the following output. It states that 97 is a prime number.
Feedback
- Send your Feedback to [email protected]