
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Number Is Prime in PHP
To check if a number is prime or not, the code is as follows ?
Example
<?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" ?>
Output
It is a prime number
A function named check_prime() is used to check if the number is prime or not. A number that needs to be checked for being prime is passed as a parameter to the function. The number is defined and this function is called by passing the number as parameter. Relevant message is displayed on the console.
Advertisements