0

What is the PHP code to check whether an entered number is prime or not.

21st Nov 2022, 2:20 PM
Arie Lou
Arie Lou - avatar
2 odpowiedzi
+ 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
21st Nov 2022, 3:04 PM
Achyuta Shrimate
Achyuta Shrimate - avatar
+ 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.
21st Nov 2022, 3:59 PM
Ipang