# Sub array sum is prime or not

Given an array and limits (lower and upper limits), check the sum of the subarray in the given limit is prime or not

Examples :

```Input  :  a[] = {1, 2, 3, 5, 5, 4, 7, 8, 9};
lower = 3, upper = 6
Output :  Yes
Explanation:- subarray is {3, 5, 5, 4} and
sum of subarray 3+5+5+4 = 17 which is prime, so
the output is yes

Input  :  a[] = {1, 6, 4, 5, 5, 4, 7, 8, 9};
lower = 2, upper = 5
Output :  No
Explanation:- subarray is {6, 4, 5, 5} and sum
of subarray 6+4+5+5 = 20 which is Not prime so the
output is No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. First calculate the sum of sub-array using upper limit and lower limit
2. Then check the sum is prime or not.
3. If it is prime then return true otherwise return false.

let’s understand this approach using code below.

## C++

 `// A Cpp program to check the given ` `// subarray is prime or not ` `#include ` `using` `namespace` `std; ` ` `  `// function to check the array ` `bool` `isPrime(``int` `a[], ``int` `lower, ` `             ``int` `upper) ` `{ ` `    ``int` `n = 0; ` ` `  `    ``// Calculate the sum of ` `    ``// the subarray ` `    ``for` `(``int` `i = lower - 1; ` `         ``i <= upper - 1; ` `         ``i++) ` `        ``n += a[i]; ` ` `  `    ``// check the sum of the ` `    ``// subarray is prime or ` `    ``// not (Corner case) ` `    ``if` `(n <= 1) ` `        ``return` `false``; ` ` `  `    ``// Check from 2 to n - 1 ` `    ``for` `(``int` `i = 2; i < n; i++) ` `        ``if` `(n % i == 0) ` `            ``return` `false``; ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// taking input ` `    ``int` `a[] = { 1, 2, 3, 5, 5, ` `                ``4, 7, 8, 9 }; ` `    ``int` `lower = 3, upper = 6; ` `    ``if` `(isPrime(a, lower, upper)) ` `        ``cout << ``"Yes"` `<< endl; ` `    ``else` `        ``cout << ``"No"` `<< endl; ` `} `

## Java

 `// A java program to check the given ` `// subarray is prime or not ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``// function to check the array ` `    ``static` `boolean` `isPrime(``int` `a[], ` `                           ``int` `lower,  ` `                           ``int` `upper) ` `    ``{ ` `        ``int` `n = ``0``; ` ` `  `        ``// Calculate the sum of ` `        ``// the subarray ` `        ``for` `(``int` `i = lower - ``1``; ` `             ``i <= upper - ``1``; i++) ` `            ``n += a[i]; ` ` `  `        ``// check the sum of the ` `        ``// subarray is prime or ` `        ``// not (Corner case) ` `        ``if` `(n <= ``1``) ` `            ``return` `false``; ` ` `  `        ``// Check from 2 to n-1 ` `        ``for` `(``int` `i = ``2``; i < n; i++) ` `            ``if` `(n % i == ``0``) ` `                ``return` `false``; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// taking input ` `        ``int` `a[] = { ``1``, ``2``, ``3``, ``5``, ``5``, ``4``, ``7``, ``8``, ``9` `}; ` `        ``int` `lower = ``3``, upper = ``6``; ` ` `  `        ``if` `(isPrime(a, lower, upper)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## Python3

 `# A Python3 program to check the given ` `# subarray is prime or not ` ` `  `# function to check the array ` `def` `isPrime(a, lower, upper) : ` `    ``n ``=` `0` ` `  `    ``# Calculate the sum of  ` `    ``# the subarray ` `    ``for` `i ``in` `range``(lower ``-` `1``, upper) : ` `        ``n ``=` `n ``+` `a[i] ` ` `  `    ``# check the sum of the  ` `    ``# subarray is prime or  ` `    ``# not (Corner case) ` `    ``if` `(n <``=` `1``) : ` `        ``return` `False` ` `  `    ``# Check from 2 to n - 1 ` `    ``for` `i ``in` `range``(``2``, n) : ` `        ``if` `(n ``%` `i ``=``=` `0``) : ` `            ``return` `False` `     `  `    ``return` `True` ` `  `# Driver Code ` ` `  `# taking input ` `a ``=` `[``1``, ``2``, ``3``, ``5``, ``5``, ``4``, ``7``, ``8``, ``9``] ` `lower ``=` `3` `upper ``=` `6` `if` `(isPrime(a, lower, upper)) : ` `    ``print` `(``"Yes"``) ` `else` `: ` `    ``print` `(``"No"``) ` ` `  `# This code is contributed by  ` `# Manish Shaw (manishshaw1) `

## C#

 `// A C# program to check the given ` `// subarray is prime or not ` `using` `System; ` ` `  `class` `GFG { ` `    ``// function to check the array ` `    ``static` `bool` `isPrime(``int``[] a, ` `                        ``int` `lower, ` `                        ``int` `upper) ` `    ``{ ` `        ``int` `n = 0; ` ` `  `        ``// Calculate the sum of ` `        ``// the subarray ` `        ``for` `(``int` `i = lower - 1; ` `             ``i <= upper - 1; ` `             ``i++) ` `            ``n += a[i]; ` ` `  `        ``// check the sum of the ` `        ``// subarray is prime or ` `        ``// not (Corner case) ` `        ``if` `(n <= 1) ` `            ``return` `false``; ` ` `  `        ``// Check from 2 to n-1 ` `        ``for` `(``int` `i = 2; i < n; i++) ` `            ``if` `(n % i == 0) ` `                ``return` `false``; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``// taking input ` `        ``int``[] a = { 1, 2, 3, 5, 5, ` `                    ``4, 7, 8, 9 }; ` `        ``int` `lower = 3, upper = 6; ` `        ``if` `(isPrime(a, lower, upper)) ` `            ``Console.Write(``"Yes"``); ` `        ``else` `            ``Console.Write(``"No"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output:

```Yes
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Sam007, jit_t, manishshaw1

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.