Solution Review 2: Check for Prime Number
This lesson provides a detailed review of the solution to the challenge in the previous lesson
We'll cover the following...
Understanding the Code
In the code above, the function isPrime is a recursive function as it makes a recursive call in the function body. Below is an explanation of the above code:
Driver Function
-
In the
main()code, we have defined an integer variable, namedinput, and boolean variable, namedresult. -
resultstores the output of theisPrimefunction. -
The first parameter in the
isPrimefunction is the number to be tested, namedinput, and the second parameter,input/2, is half of the number to be tested. -
lines 31-34 and lines 36-39 prints the type, prime or not prime, of the number based on the value of
result.
Recursive Function
-
The return type of this function is
boolsince we want to check if the number is prime or not and it takes the two integers,nandi, as input parameters. -
nis the number to be tested andiis the iterator and has the initial value ofn/2when the function is called in the main code. -
ihas the initial value of ...