Recursive program for prime number | CPP Program to find whether a Number is Prime or Not using Recursion ; Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n , int i = 2 ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } int main ( ) { int n = 15 ; if ( isPrime ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }