0
What is the PHP code to check whether an entered number is prime or not.
2 Answers
+ 2
Well I haven't learnt php, but the basic idea for checking number is prime or not in simple algorithm.
Algorithm:
Step 1: Take a variable c (counter).
Step 2: Input the number to be checked.
Step 3: Take a loop starting from 1, increasing by 1 all the way up to the number itself.
Step 4: In the loop check if the number is completely divisible by loop variable, i.e. check if the remainder is 0 using modulus function, if yes then increase the counter by 1.
Step 5: After the execution of loop check if the value of counter equal to 2, if yes then it is prime else composite.
Hope it helps you
+ 1
Go to Create -> Code section
Switch language filter on to PHP
Enter 'prime' (without quotes) in the search bar and you'll get plenty of code examples to learn from.