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
#include <iostream>using namespace std;bool isPrime(int n, int i){// first base caseif (n<2){return 0;}// second base caseif(i==1){return 1;}// third base caseif (n%i==0){return 0;}// recursive caseelse{isPrime(n,i-1);}}int main() {int input= 13;bool result= isPrime(input,input/2);// prints if number is primeif (result==1){cout<<input<<" is a prime number.";}//prints if number is not primeelse{cout<<input<<" is a not a prime number.";}}
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.