Question

Write a PHP function to check if a number in input is prime or not. Add all the necessary checks to the input. Note: A prime
0 0
Add a comment Improve this question Transcribed image text
Answer #1

<?php


// Utility function for checking whether number is prime or not


function checkIfPrime($numValue){
if ($numValue == 1)
return 0;
  
for ($i = 2; $i <= sqrt($numValue); $i++){
if ($numValue % $i == 0)
return 0;
}
return 1;
}
  
// Enter number which you want to check
$numValue = 31;

// Checking Valid Natural Number
if($numValue > 0){
$isPrime = checkIfPrime($numValue);
if ($isPrime == 1)
echo "$numValue is Prime";
else
echo "$numValue is Not Prime";
}
else{
echo "Please Enter Natural Number";
}


?>

Add a comment
Know the answer?
Add Answer to:
Write a PHP function to check if a number in input is prime or not. Add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • For Python: A prime number is defined as an integer greater than 1 that has no...

    For Python: A prime number is defined as an integer greater than 1 that has no positive divisors other than 1 and itself. Write a program that prompts the user for an integer > 1. Validate the value is > 1 (if not, ask for another). Use a loop to determine if the number is prime or not. Issue an appropriate message. [complete this part before proceeding]. Add a loop that continues to ask the user if they would like...

  • A positive integer greater than 1 is said to be prime if it has no divisors...

    A positive integer greater than 1 is said to be prime if it has no divisors other than 1 and itself. A positive integer greater than 1 is composite if it is not prime. Write a program that defines two functions is_prime() and list_primes(). is_prime() will determine if a number is prime. list_primes() will list all the prime numbers below itself other than 1. For example, the first five prime numbers are: 2, 3, 5, 7 and 11." THE PROGRAM...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • Design a circuit that has a 3 bit binary input (representing 0 through 7) and outputs...

    Design a circuit that has a 3 bit binary input (representing 0 through 7) and outputs a 1 if the input is a prime number.  A prime number (or prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. a)  Fill out a truth table that represents the logic equation for this circuit:  Y = F(A,B,C). b)  Using a Karnaugh map simplify the logic equation.

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • Please make sure to write the test function! Using PHP, design a function that given a...

    Please make sure to write the test function! Using PHP, design a function that given a Roman numerale in input, is able to compute the modern Hindu-Arabic numeral system representation (aka, 0123456789). For example: Output Input VI IV MCMXC 1990 • Be sure to add all the necessary checks and at least one testing function, • Try to split the logic in small functions. What is a "check"? If you have a function that accepts an input, you want to...

  • In ASCII C programming write a function that determines if an integer is prime. A prime...

    In ASCII C programming write a function that determines if an integer is prime. A prime number is one that is not divisible by any number other than one and itself. int isPrime(long num); Return 1 if num is prime, 0 if not. As above, make this a pure function contained in its own file isPrime.c and test it with a separate tester program. To test if an integer is prime you could implement a loop that generates trial divisors...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Q2. To check and print if entered integer is prime or not. Prompt the user to...

    Q2. To check and print if entered integer is prime or not. Prompt the user to enter a positive integer or 0 to exit. while negative integer entered - prompt for valid values again. If positive integer entered - check if it is prime. Write a separate function that accepts a positive integer as argument and checks and returns 1 if the integer is prime or 0 if it is not prime. Call the function from main. Accept the return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT